0

I'm having a little issue. I'm developing an app and I created API with node.js(using express). Right now I'm trying to send my error objects from node to react but for some reason I cannot get it. I can see the object in the network tab but I want to use it, like console it to the client.

back-end:

app.post('/api/users/login', async (req, res) => {
  try {
    const user = await User.findByCredentials({ ...req.body });
    const token = await user.generateAuthToken();
    res
      .cookie('w_auth', token)
      .status(200)
      .send({ user, token });
  } catch (error) {
    res.status(400).send({ success: false, error: 'some error' });
  }
});


client-side:

    loginUser:
    export const loginUser = dataToSubmit => {
    return axios.post(`${USER_SERVER}/login`, dataToSubmit);
    };

loginUser(dataToSubmit)
        .then(res => {
          console.log(res);
          dispatch({ type: 'SET_USER', user: res.data.user });
        })
        .catch(error => {
          console.log(error);
        });

I tried also just send a respond without error from back-end which also didn't work.

picture of what I get: enter image description here network tab: enter image description here

rio
  • 757
  • 5
  • 19
  • 32

1 Answers1

0

You can catch it the below way.

axios
  .post(url, data)
  .then(response => {
    //You get success response here.
  })
  .catch(err => {
    //Error response here
  });
Dinesh Kumar
  • 478
  • 4
  • 16