【发布时间】:2017-08-04 22:38:03
【问题描述】:
所以在我的反应代码中我有这个:
componentDidMount = () => {
firebase.auth().getRedirectResult().then(function(result) {
if (result.credential) {
var token = result.credential.accessToken;
}
var ruser = result.user;
console.log(ruser.displayName, 'lol'); //THIS LOGS CORRECTLY
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;
}).then(function(result){
this.setState({user: ruser});
console.log(result, 'rslt'); //THIS LOGS UNDEFINED
});
}
我基本上想设置登录用户的状态。但是,当我把它传递下来时,我得到了未定义,我在承诺方面做错了什么?我只想传递结果并使其在函数范围之外可用...
【问题讨论】:
-
您可能需要在您的第一个
then中使用return result/ruser。 -
另外,
this不会在您最后的then回调中定义。您需要使用箭头函数或将this绑定到回调。
标签: javascript reactjs firebase promise firebase-authentication