I am working on Reactjs redux on the front-end and Rails api as the back-end.
So now I am calling the API using the Fetch API method, but the problem is that I cannot get a readable error message similar to the one I received on the network tabs
this is my function
export function create_user(user,userInfoParams={}) {
return function (dispatch) {
dispatch(update_user(user));
return fetch(deafaultUrl + '/v1/users/',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify(userInfoParams)
})
.then(function(response) {
console.log(response);
console.log(response.body);
console.log(response.message);
console.log(response.errors);
console.log(response.json());
dispatch(update_errors(response));
if (response.status >= 400) {
throw new Error("Bad response from server");
}
})
.then(function(json){
console.log("succeed json re");
dispatch(update_user(json));
});
}
}
But when errors occurred, I can’t understand how to get a readable response message that I receive when I check the network tabs of my browser
So this is what I got from the network tabs when I got errors.

My console

This is the code for my rail
def create
user = User.new(user_params)
if user.save
render json: user, status: 201
else
render json: { errors: user.errors }, status: 422
end
end
But I can’t understand how I can get this in my function
Thank!
source
share