javascript - React.js - When authorizing on the site, if the data is incorrect, how can I get a response with a text error? -


i send data api email , password. api ruby on rails library devise-token-auth


signinform.js

......................................................

onsubmit(e){ e.preventdefault(); this.setstate({errors: {}, isloading: true}); this.props.usersigninrequest(this.state).then(   () => {console.log('ok!')},   ({resp}) => { console.log(resp.errors); } ); 

} .........................................................

actions/signinactions.js

import axios 'axios';  export function usersigninrequest(userdata){   return dispatch => {     return axios.post('/auth/sign_in', userdata);   } } 

if email or password incorrect response:

{"errors":["invalid login credentials. please try again."]} 

but console.log(resp.errors); display undefined. if put (resp) => { console.log(resp); } in console:

error: request failed status code 401 @ createerror (createerror.js?16d0:16) @ settle (settle.js?db52:18) @ xmlhttprequest.handleload (xhr.js?ec6c:77)

how me errors in console.log(resp);

that because way have written resp in argument. have written resp in bracket {resp}. means whenever object passed function, resp equal resp property of object. response have errors property , no resp property, shows undefined.

so work can either

onsubmit(e){ e.preventdefault(); this.setstate({errors: {}, isloading: true}); this.props.usersigninrequest(this.state).then(   () => {console.log('ok!')},   (resp) => { console.log(resp.errors); } ); 

or

onsubmit(e){ e.preventdefault(); this.setstate({errors: {}, isloading: true}); this.props.usersigninrequest(this.state).then(   () => {console.log('ok!')},   ({errors}) => { console.log(errors); } ); 

edit:

this.props.usersigninrequest(this.state).then(() =>{     console.log('ok!') }).catch((resp) => {     console.log(resp.response.data); })); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -