Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarification on how to use onSuccess / onError with mutation #38

Closed
cn-johndpope opened this issue Apr 11, 2023 · 1 comment
Closed

Comments

@cn-johndpope
Copy link

is it possible to pass this into the given mutations?

wanting to do something like this.... at client level - without rewriting sdk

https://github.com/c121914yu/FastGPT/blob/8c70205940138c75e48fa648b927fafeea1f0e5a/src/hooks/useRequest.tsx

this runs / authenticates fine - but I need to save the token to add to auth header.

           var model:UserLoginModel = {}
            model.userName = username
            model.password = password
            await postMutation.mutate(model)

this code compiles and runs - but I don't see any onSuccess callback.

           var model:UserLoginModel = {}
            model.userName = username
            model.password = password
            await postMutation.mutate(model,{
                onError: (error:any, variables:any, context:any) => {
                    // An error happened!
                    console.log(`🔥 error ${error}`)
                  },
                  onSuccess: (data:any, variables:any, context:any) => {
                    console.log("🎸  data:",data)
                    console.log("variables:",variables)
                    console.log("context:",context)
        
                    // let token = data.token
                    // axiosInstance.defaults.headers.common.Authorization = `Bearer ${token}`;
                    // Boom baby!
                  },
            })

// here we are authenticated - but in the body of response - there's a token
the body - is by default undefined - so there's no token - auto populated...
console.log("🌱 success result:",postMutation)
console.log("🌱 success result:",postMutation.result.token ?? "") // FAILS - as the postMutation . data = undefined

🌱 success result: {"context": undefined, "data": undefined, "error": null, "failureCount": 0, "failureReason": null, "isError": false, "isIdle": true, "isLoading": false, "isPaused": false, "isSuccess": false, "mutate": [Function anonymous], "mutateAsync": [Function bound mutate], "reset": [Function bound reset], "status": "idle", "variables": undefined}

can tanstack be made to default to any - instead of undefined?

@rametta
Copy link
Owner

rametta commented Apr 11, 2023

This seems unrelated to Rapini. For general support for Tanstack please file issues in their repo. For axios issues file an issue in the axios repo. Thanks

@rametta rametta closed this as completed Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants