2017-10-31 16 views
0

그래서 나는이 코드를 가지고 :(반작용 네이티브) 가능한 처리되지 않은 약속 거부 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> 
    ); 
    } 
} 

나는 그것이 성공의 페이스 북으로 로그인을 시도 할 때마다. 하지만 난 항상 경고를 말한다 얻을

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

나는 두 기능 _fbAuth을 결합하고 생성자에 로그인을 시도하지만 여전히 같은 경고를 줄입니다.

답변

1

다음 내부 함수를 바인딩해야합니다. 그것은 일의 약속

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

사용하는 경우 또 다른 좋은 연습 catch을 사용

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

또는 사용할 수있는 화살표 기능

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

! 대단히 감사합니다. _fbAuth에 대한 바인딩만으로도 충분합니다. –