2017-10-31 3 views
0

So habe ich diesen Code:(React-Muttersprache) Möglich Unhandle Versprechen Ablehnung FBSDK

export default class Login extends Component { 

    constructor(props){ 
    super(props); 
    this._fbAuth = this._fbAuth.bind(this); 
    this.login = this.login.bind(this); 
    } 

    login(){ 
     this.props.navigator.push({ 
     id: "Home", 
     }); 
    } 

    _fbAuth(){ 
    LoginManager.logInWithReadPermissions(['public_profile']).then(
     function(result) { 
     if (result.isCancelled) { 
      alert('Login cancelled'); 
     } else { 
alert('Login success with permissions: '+result.grantedPermissions.toString()); 
      this.login(); 
     } 
     }, 
     function(error) { 
     alert('Login fail with error: ' + error); 
     } 
    ); 
    } 

    render() { 
    return (
     <View style={styles.container}> 
       <View style={styles.botbuttoncontainer}> 
       <Text style={styles.otherlogintext}>Or log in using</Text> 
       <View style={styles.otherloginbutton}> 
       <TouchableOpacity style={styles.facebookbutton} activeOpacity={0.5} onPress={()=>this._fbAuth()}> 
        <Icons name="logo-facebook" size={20} color="white"/> 
       </TouchableOpacity> 
       <TouchableOpacity style={styles.twitterbutton} activeOpacity={0.5}> 
        <Icons name="logo-twitter" size={20} color="white"/> 
       </TouchableOpacity> 
       <TouchableOpacity style={styles.googlebutton} activeOpacity={0.5}> 
        <Icons name="logo-googleplus" size={20} color="white"/> 
       </TouchableOpacity> 
       </View> 
       </View> 
     </View> 
    ); 
    } 
} 

Jedes Mal, wenn ich versuche, mit Facebook einzuloggen es erfolgreich ist. aber ich bekomme immer eine Warnung sagt

"Possible Unhandled Promise Rejection(id:0): TypeError: undefined is not a function (evaluating 'this.login()')"

Ich versuche, sowohl Funktion zu binden _fbAuth und Anmeldung auf Konstruktor, aber es ist immer noch die gleiche Warnung geben.

Antwort

1

Sie müssen innere Funktion des Anrufs dann binden.

Beispiel

LoginManager.logInWithReadPermissions(['public_profile']).then(
    function (result) { 
    if (result.isCancelled) { 
     alert('Login cancelled'); 
    } 
    else { 
     alert('Login success with permissions: ' + result.grantedPermissions.toString()); 
     this.login(); 
    } 
    }.bind(this), 
    function (error) { 
    alert('Login fail with error: ' + error); 
    } 
); 

Oder Sie können Pfeil-Funktionen verwenden

LoginManager.logInWithReadPermissions(['public_profile']).then(
    (result) => { 
    if (result.isCancelled) { 
     alert('Login cancelled'); 
    } 
    else { 
     alert('Login success with permissions: ' + result.grantedPermissions.toString()); 
     this.login(); 
    } 
    }, 
    function (error) { 
    alert('Login fail with error: ' + error); 
    } 
); 

Versprechen Eine weitere gute Praxis catch verwendet bei der Verwendung von

Beispiel

LoginManager.logInWithReadPermissions(['public_profile']).then(
    (result) => { 
    if (result.isCancelled) { 
     alert('Login cancelled'); 
    } 
    else { 
     alert('Login success with permissions: ' + result.grantedPermissions.toString()); 
     this.login(); 
    } 
    }, 
    function (error) { 
    alert('Login fail with error: ' + error); 
    } 
).catch((error) => console.error(error)); // error handling for promise 
+0

Arbeit Es ist! Vielen Dank, mein Herr. Zuerst bin ich bind auf _fbAuth ist genug. –

Verwandte Themen