gpt4 book ai didi

javascript - 未定义未处理的 Promise Rejection

转载 作者:行者123 更新时间:2023-12-03 03:43:51 42 4
gpt4 key购买 nike

在我的 React Native 应用程序中,我有一个自定义登录 Facebook 按钮:

<Button onPress={() => this.handleFacebookLogin()}>
<Text>Login with Face</Text>
</Button>

还有handleFacebookLogin函数:

 handleFacebookLogin () {
LoginManager.logInWithReadPermissions(['public_profile', 'email', 'user_friends']).then(
function (result) {
if (result.isCancelled) {
console.log('Login cancelled')
} else {
console.log('Login success with permissions: ' + result.grantedPermissions.toString())
AccessToken.getCurrentAccessToken().then(
(data) => {
signInFacebookLoginInFirebase(data.accessToken)
//this.signInFacebookLoginInFirebase(data.accessToken)
}
)
}
},
function (error) {
console.log('Login fail with error: ' + error)
alert('Error at login, no network ?')
}
)
}

但我收到此错误:

Possible Unhandled Promise Rejection (id: 20): ReferenceError: signInFacebookLoginInFirebase is not defined TypeError: _this2.signInFacebookLoginInFirebase is not a function

类型错误:_this2.signInFacebookLoginInFirebase 不是函数

和signInFacebookLoginInFirebase方法:

   signInFacebookLoginInFirebase(facebookToken){
const credential = Fb.firebase.auth.FacebookAuthProvider.credential(facebookToken);
Fb.firebase
.auth()
.signInWithCredential(credential)
.then(() => alert('Account accepted'))
.catch((error) => alert('Account disabled'));
}

最佳答案

如果您的 .then 回调不使用箭头符号来 logInWithReadPermissionsthis 将不会成为您的 signInFacebookLoginInFirebase< 的上下文 函数

您在AccessToken.getCurrentAccessToken().then中使用箭头表示法,这样很好,现在您只需更改

LoginManager.logInWithReadPermissions(['public_profile', 'email', 'user_friends']).then(
function (result) {

LoginManager.logInWithReadPermissions(['public_profile', 'email', 'user_friends']).then(
(result) => {

然后使用注释掉的

this.signInFacebookLoginInFirebase(data.accessToken);

为了正确处理所有潜在的拒绝,我建议

handleFacebookLogin () {
LoginManager.logInWithReadPermissions(['public_profile', 'email', 'user_friends'])
.then(result => {
if (result.isCancelled) {
console.log('Login cancelled');
} else {
console.log('Login success with permissions: ' + result.grantedPermissions.toString())
return AccessToken.getCurrentAccessToken()
.then(data => this.signInFacebookLoginInFirebase(data.accessToken));
}
// .catch chained from .then to handle all rejections
}).catch(error => {
console.log('Login fail with error: ' + error);
alert('Error at login, no network ?');
})
}

关于javascript - 未定义未处理的 Promise Rejection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45496011/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com