我想将credential.access令牌存储在变量中,我尝试了以下操作,但未成功

时间:2018-10-31 18:55:35

标签: javascript function store access-token

var Token; 
componentDidMount() {
  auth0.webAuth.authorize({
    scope: 'openid profile email',
    audience: 'https://MyDomain/userinfo' 
   })
   .then(credentials => 
      console.log(credentials.accessToken))
      this.key=credentials.accessToken;
 }

2 个答案:

答案 0 :(得分:0)

从ComponentDidMount看来这是个反应。

您似乎不是在回调内部执行this.key=credentials.accessToken,而是在外部执行。这意味着当您尝试设置this.key = credentials.accessToken;

时,凭据.accessToken将是未定义的。

答案 1 :(得分:0)

尝试一下

var Token; 
componentDidMount() {
  auth0.webAuth.authorize({
    scope: 'openid profile email',
    audience: 'https://MyDomain/userinfo' 
   })
   .then(credentials => {
      console.log(credentials.accessToken);
      this.key=credentials.accessToken });
 }