Xero
Xero

Reputation: 4175

Unhandled Promise Rejection is not defined

In my React Native App, I have a custom login facebook button :

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

And the handleFacebookLogin function :

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

But I get this error :

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

TypeError: _this2.signInFacebookLoginInFirebase is not a function

And signInFacebookLoginInFirebase method :

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

Upvotes: 0

Views: 4316

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

By not using arrow notation for your .then callback to logInWithReadPermissions, this will not be the context of your signInFacebookLoginInFirebase function

you use arrow notation in AccessToken.getCurrentAccessToken().then so that's good, now you just simply change

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

to

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

and then use the commented out

this.signInFacebookLoginInFirebase(data.accessToken);

To properly handle all potential rejections, I would recommend

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

Upvotes: 1

Related Questions