Кеняка
Кеняка3mo ago

invalid args for renaming a file

when calling a function to rename a file, it says that the arguments are invalid, although I specified everything that is needed
await renameFile({
fileId: file.fileId,
name: values.title,
})
await renameFile({
fileId: file.fileId,
name: values.title,
})
export const renameFile = mutation({
args: {
fileId: v.id("files"),
name: v.string()
},
async handler(ctx, args){
const access = await hasAccessToFile(ctx, args.fileId)

if(!access){
throw new ConvexError("no access to file")
}

await ctx.db.patch(args.fileId, {
name: args.name
})

}
})
export const renameFile = mutation({
args: {
fileId: v.id("files"),
name: v.string()
},
async handler(ctx, args){
const access = await hasAccessToFile(ctx, args.fileId)

if(!access){
throw new ConvexError("no access to file")
}

await ctx.db.patch(args.fileId, {
name: args.name
})

}
})
No description
No description
No description
17 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Clever Tagline
Clever Tagline3mo ago
Your mutation definition is incorrect. When defining the handler, it should begin like this:
handler: async (ctx, args) => { ...
handler: async (ctx, args) => { ...
What the mutation function receives is an object, so args and handler both must be properties of that object. Your current syntax doesn't follow that pattern. Frankly I'm not sure why you didn't get an error about this.
Кеняка
КенякаOP3mo ago
here it requires specifying the ctx argument, provided that in all other functions when they are called, it does not require
Кеняка
КенякаOP3mo ago
No description
No description
Clever Tagline
Clever Tagline3mo ago
I don't think you understand my point. You're defining the handler like this:
async handler(ctx, args) { ... }
async handler(ctx, args) { ... }
...when it should look like this:
handler: async (ctx, args) => { ... }
handler: async (ctx, args) => { ... }
handler needs to be a property on the object that's passed to mutation, just as args above it is another property in that object. Passing the function any other way won't work.
Кеняка
КенякаOP3mo ago
OK, I change the handler for the handler method, but it still complains about the 2nd argument
Кеняка
КенякаOP3mo ago
No description
No description
Clever Tagline
Clever Tagline3mo ago
How are you defining renameFile? Do you have a renameFile function defined anywhere else?
Кеняка
КенякаOP3mo ago
Creating an arrow function
export const renameFile = mutation({
...
})
export const renameFile = mutation({
...
})
Clever Tagline
Clever Tagline3mo ago
Sorry, I meant in the component where you're trying to use it.
Кеняка
КенякаOP3mo ago
convex/files.ts
Clever Tagline
Clever Tagline3mo ago
In the file where you're seeing that error, where is renameFile defined?
Кеняка
КенякаOP3mo ago
import { renameFile } from "../../../../convex/files" Import it finds all other functions from the same file too
Clever Tagline
Clever Tagline3mo ago
That's not the correct way to use the function. You should be importing the useMutation hook and doing something like this:
const renameFile = useMutation(api.files.renameFile)
const renameFile = useMutation(api.files.renameFile)
https://docs.convex.dev/functions/mutation-functions#calling-mutations-from-clients
Mutations | Convex Developer Hub
Mutations insert, update and remove data from the database, check authentication
Кеняка
КенякаOP3mo ago
OOPS I called other functions using this hook, but then I forgot
Clever Tagline
Clever Tagline3mo ago
No worries. It can take some time to get used to the Convex workflow.
Кеняка
КенякаOP3mo ago
thanks

Did you find this page helpful?