#function

1 messages · Page 1 of 1 (latest)

stiff badger
#

@sturdy tangle does this function look correc tto you?

export const createMultipleTasks = authMutation({
  args: { 
    tasks: v.array(v.object({
      name: v.string(),
      frequency: v.number(),
      weekday: v.number()
    }))
  },
  handler: async (ctx, args) => {
    const newTasks = args.tasks.map(async (task) => {
      const newTask = await ctx.db.insert("tasks", {
        name: task.name,
        frequency: task.frequency,
        weekday: task.weekday,
      })
      return newTask
    })

    return newTasks
  },
})
sturdy tangleBOT
#

Hi @stiff badger. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The function you've written seems to be a mutation function in Convex that is designed to create multiple tasks at once. However, there are a few things to note:

  1. The function authMutation is not mentioned in the provided knowledge sources. It's likely that you meant to use mutation instead, which is a function provided by Convex for creating mutations.

  2. When using Array.prototype.map with async functions, you're creating an array of promises. To wait for all these promises to resolve, you should use Promise.all.

Here's how you might adjust your function:

export const createMultipleTasks = mutation({
  args: { 
    tasks: v.array(v.object({
      name: v.string(),
      frequency: v.number(),
      weekday: v.number()
    }))
  },
  handler: async (ctx, args) => {
    const newTasks = await Promise.all(args.tasks.map(async (task) => {
      const newTask = await ctx.db.insert("tasks", {
        name: task.name,
        frequency: task.frequency,
        weekday: task.weekday,
      })
      return newTask
    }))

    return newTasks
  },
})

Please note that this is based on the information available in the provided knowledge sources. You should test this function in your own environment to ensure it works as expected.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: