【问题标题】:Unhandled Promise Rejection is not defined未定义未处理的 Promise Rejection
【发布时间】:2018-01-11 18:07:44
【问题描述】:

在我的 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 ?')
       }
     )
 }

但我收到此错误:

可能的未处理承诺拒绝(id:20):ReferenceError: 未定义 signInFacebookLoginInFirebase 类型错误:_this2.signInFacebookLoginInFirebase 不是函数

TypeError: _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'));
   }

【问题讨论】:

  • 那么你有一个叫做signInFacebookLoginInFirebase的函数吗?
  • 注释掉了错误的行?
  • 我用“this”试过了,但没有用
  • function (result) 更改为(result) =&gt; ... 现在this 可能是正确的,所以您可以使用this.signInFacebookLoginInFirebase - 也许?从您的不同代码片段中不清楚
  • 它正在工作,谢谢@JaromandaX,如果你发布一个,我赞成你的回答

标签: javascript react-native es6-promise


【解决方案1】:

如果您的 .then 回调到 logInWithReadPermissions 不使用箭头符号,this 将不会是您的 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 ?');
    })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2018-03-19
    • 2018-04-12
    • 2018-08-06
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多