0

Sup guys! This is the code on server side when something goes wrong:

return res.status(400).send({ error: 'Email already used!'});

And this is my frontend code that I try to catch the error message:

return async dispacth => {
        try {

            const res = await axios.post('http://localhost:3333/account/signin', userData);

            localStorage.setItem('JWT_TOKEN', res.data.token);

            dispacth({
                type: AUTH_SIGN_IN,
                token: res.data.token
            });

        } catch (err) {
            console.log(err.message)
            console.error('error', err);

            dispacth({
                type: AUTH_ERROR,
                errorMessage: err.message
            });
        }
    }

But this is what I got on browser's console:

enter image description here

1
  • 1
    Try err.response.body.error Commented May 3, 2020 at 1:48

2 Answers 2

2

Try console.log(err.response) instead of console.log(err.message).

I'm new to Node and Axios, but using "err.response" after catching "err" fixed a very similar issue I had.

Sign up to request clarification or add additional context in comments.

Comments

1

For res.status(400).send('Email already used!');

You could do:

console.log(error.response.data.message);

For the error message being sent as a JSON object like res.status(400).send({ error: 'Email already used!'});

You could do:

console.log(error.response.data.error);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.