I am using RTK query for comunication with API. When mutation fails, I need dispatch some alert, like follows:
const [addPost, {
isError,
error,
}] = useAddPostMutation()
...
useEffect(() => {
if (isError) {
dispatch(setAlert(error))
}
}, [isError])
It works ok.
But now, I would like to omit useEffect
and I found in official docs (https://redux-toolkit.js.org/rtk-query/usage/error-handling) this code.
addPost({ id: 1, name: 'Example' })
.unwrap()
.then((payload) => console.log('fulfilled', payload))
.catch((error) => console.error('rejected', error))
then
block works ok, but there is some problem with catch
block. I got this error message:
rejected TypeError: description(...) is undefined
I tried it without unwrap()
, it falls every time into then
block and get same error.
addPost({ id: 1, name: 'Example' })
.then((payload) => console.log('fulfilled', payload))
.catch((error) => console.error('rejected', error))
fulfilled
Object { error: TypeError }