简体   繁体   English

放入新的未定义对象Error(error.response);

[英]Undefined object when putting in throw new Error(error.response);

I would like to ask if why my object is getting undefined when I put it inside the throw new Error(error.response) but if outside I can get the exact value. 我想问一下,当我将对象放入throw new Error(error.response)内时,为什么我的对象变得不确定,但是如果在外面,我可以得到确切的值。 Here's my code below: 这是我的代码如下:

componentDidMount(){
   this.validateUrlInToken()
    .then(this.onValidateUrlInTokenSuccess)
    .catch(this.onValidateUrlInTokenFail);
}

validateUrlInToken() {
  const tokenParams = {
    token: this.props.params.token,
  };

  return axios.post('/api/core/validate_token/', tokenParams);
}

onValidateUrlInTokenSuccess(response) {
  const { data } = response.data;
  const email = data.username;
  const userId = data.user_id;
  const lastLogin = data.last_login;

  this.setState({ email, userId, lastLogin });

  this.getSite()
    .then(this.onGetSiteSuccess)
    .catch(this.onGetSiteFail);
}

onValidateUrlInTokenFail(error) {
  console.log(error.response) // { data: { detail: 'Invalid Token' } }
  this.context.router.push('/login');
  throw new Error(error.response); // Undefined response
}

try using callback instead 尝试改用回调

onValidateUrlInTokenFail(callback,error) {
  console.log(error.response) // { data: { detail: 'Invalid Token' } }
  this.context.router.push('/login');
  callback(new Error(error.response));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM