T
TanStack•3y ago
flat-fuchsia

How to handle error in react-query's onSuccess event?

I want to fire a function in onSuccess event but this function can failed. What is the best to handle this?
8 Replies
quickest-silver
quickest-silver•3y ago
What should happen if it fails?
flat-fuchsia
flat-fuchsiaOP•3y ago
retry the function and if it fails again modify the database
quickest-silver
quickest-silver•3y ago
Why not use the retry option?
flat-fuchsia
flat-fuchsiaOP•3y ago
because if mutation finished ok i dont want to create a new record again i want to excute the function after de mutation again
quickest-silver
quickest-silver•3y ago
first time you're mentioning it's about mutations ....
because if mutation finished ok i dont want to create a new record again
i still don't understand this, sorry. can you try being a bit more descriptive? the retry function (on mutations and queries) is called when the actual mutationFn (or queryFn) fails. So if it succeeds, it will not be called. But it will be called if it fails I think I misread the question but it's hard to decipher 😅
flat-fuchsia
flat-fuchsiaOP•3y ago
Sody if my english is not the best I am excuting a function inside on sucess:
useMutation({
mutationFn: async ({
salaPath, text, userName, uid, mediaUri = null,
}) => {
firebasFunction()
},
onSucess: async () => {
//If the record is correctly added to the databse i execute a new function
await functionB()
}
useMutation({
mutationFn: async ({
salaPath, text, userName, uid, mediaUri = null,
}) => {
firebasFunction()
},
onSucess: async () => {
//If the record is correctly added to the databse i execute a new function
await functionB()
}
The functionB can fail and i want to retry it 2 times I it continues failing i need to update the database again
quickest-silver
quickest-silver•3y ago
is functionB also a mutation?
flat-fuchsia
flat-fuchsiaOP•3y ago
no but if retries 3 times and it doesnt finish correctly i want to save a variable in the database (a mutation) @TkDodo 🔮 Sory i bother you but i couldnt found a good approach

Did you find this page helpful?