I created my store with Redux-Toolkit and my slice. Now wanting to retrieve the data by firebase I created an async thunk by createAsyncThunk
. I want to register a user by email and password. The registration is ok, but when I want to register the same user again I cannot capture the errors to display them.
export const signUp = createAsyncThunk('signUp', async (user) => {
const {email, password} = user
const response = await fetch('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=<KEY VALUE REDACTED>', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, password })
});
if (!response.ok) {
console.log("nous")
throw new Error("Nous avons un problème lors de l'inscription")
}
const data = response.json()
return data
})
const handleSubmit = async () => {
if (email.length > 0 && password.length > 0) {
if (isSignUp) {
setError(null)
try {
dispatch(signUp({ email, password }))
} catch (error) {
console.log(error)
}
} else {
// alert('Veuillez remplir tous les champs')
}
}
}
2
Answers
just catch error from fetch i.e. change code from
to this
createAsyncThunk
actions always resolve, so there’s a little bit of a refactor you can do to your code to make working with errors better.try/catch
.response.json()
Promise to resolve.