2

export const register = (user, callback, errorback) => async dispatch => {
  try{

    let response = await axios.post(`${PINGUIN_ROOT_URL}/users/create`, user)

    if (response.data.auth_token){
      auth_token = response.data.auth_token
      dispatch({ type: REGISTER_SUCCESS, payload: auth_token})
      callback()
    } else {
      let error = response
      throw error
    }

  }catch(error){

    dispatch({type: REGISTER_FAIL})
    errorback()

  }

Hi, I am building a login register based off of what we have learned. It works but for some reason the error validations wont come back. I built a rails api and I see the validation errors when I use postman but when I try to get the errors back using redux the register function above gets to the "let response = .." line and immediately goes to the catch(error) line. I dont know how to pass back the actual validation errors that I see when I use post man because the error that is being caught is the following:

Error: Request failed with status code 422
    at createError (createError.js:16)
    at settle (settle.js:18)
    at XMLHttpRequest.handleLoad (xhr.js:77)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:538)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
    at XMLHttpRequest.js:485
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:250)
    at MessageQueue.js:101

Now again, the code is working when it actually logs in the user however it fails to actually give me the validation errors that I need. I see the validation errors comming back as json in postman but i do not get to see them in practice. Help please?

1 Answer 1

3

You can get the response object from your error object as error.response

try{
    let response = await axios.post(`${PINGUIN_ROOT_URL}/users/create`, user)
    ...
  } catch(error){
    console.error(error.response)
}
Sign up to request clarification or add additional context in comments.

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.