[SOLVED] useMutation does not seem to recognize all of my parameters in my function.
Hi, so I'm trying to post form data to the db and I'm having issues.
In my api folder, I have a handler function to create an entry:
Then I have a function utilizing the fetch API for the handler function posted above.
Now in the useMutation, I have this custom hook:
Now in react-hook-form, when I try to pass in the table name in the onSubmit function like so:
I get an error from prisma that says: Encountered an error during creation: {table: 'table-name'}. Invalid prisma.formTable.create() invocation. Argument 'data' is missing.
The super perplexing thing is if I use that function that utilized the fetch API in a custom hook without using React Query, then it posts to the db perfectly. Another weird thing is if I change that parameter in that body to , the error I get then recogizes the second parameter, only it thinks it is empty. I've been stuck on this for days, so any help is appreciated. I thought that maybe prisma didn't play nice with react-query, but I'm not seeing anything that says so in the docs.
5 Replies
crude-lavender•16mo ago
mutationFn: () => postToDb()
constant-blueOP•16mo ago
I tried this earlier and I got the same error
crude-lavender•16mo ago
Read the documentation of useMutation.mutationFn
constant-blueOP•16mo ago
I got it to work by moving the “table” parameter to usePostToDB, so the mutationFn changed to (form) => postToDB(table, form); only that’s really not ideal because this hook was used in a lot of places already, and I only wanted to alter the logic to use react query.
constant-blueOP•16mo ago
Thanks for the help @M00LTi
For anyone else that faces a similar problem, mutationFn does not take multiple parameters. The workaround is to wrap them in an object. Link: https://tkdodo.eu/blog/mastering-mutations-in-react-query#mutations-only-take-one-argument-for-variables
Mastering Mutations in React Query
Learn all about the concept of performing side effects on the server with React Query.