未处理未承诺的承诺拒绝

时间:2017-08-03 23:20:01

标签: javascript react-native es6-promise

在我的React Native App中,我有一个自定义登录facebook按钮:

fileprivate var timer ... //timer logic here

@objc public class CatchAllGesture : UIGestureRecognizer {
    override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
    }
    override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        //reset your timer here
        state = .failed
        super.touchesEnded(touches, with: event)
    }
    override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesMoved(touches, with: event)
    }
}

@objc extension YOURAPPAppDelegate {

    func addGesture () {
        let aGesture = CatchAllGesture(target: nil, action: nil)
        aGesture.cancelsTouchesInView = false
        self.window.addGestureRecognizer(aGesture)
    }
}

还有handleFacebookLogin函数:

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

但是我收到了这个错误:

  

可能未处理的Promise拒绝(id:20):ReferenceError:   signInFacebookLoginInFirebase未定义   TypeError:_this2.signInFacebookLoginInFirebase不是函数

TypeError:_this2.signInFacebookLoginInFirebase不是函数

并且signInFacebookLoginInFirebase方法:

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

1 个答案:

答案 0 :(得分:1)

如果对.then的{​​{1}}回调不使用箭头符号,则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);