[英](React-Native) Possible Unhandled Promise Rejection 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>
);
}
}
每次我尝试登录Facebook都成功。 但我总是得到警告说
“可能的未处理的承诺拒绝(id:0):TypeError:未定义不是一个函数(评估'this.login()')”
我尝试同时绑定函数_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();
}
}.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);
}
);
另一个好的做法是在使用Promise时使用catch
例
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.