我正在使用FacebookAuthProvider
by firebase从我的平台登录用户。
我正在firestore的expo中使用react native,在我尝试添加一些检查以将用户登录后重定向到正确的屏幕之前,它一直运行良好。登录后,有两个不同的角色(管理员和用户)必须分开。
if (/* user is administrator */) {
this.props.navigation.navigate('Admin');
} else {
this.props.navigation.navigate('Main');
}
将此方法添加到按角色分开的用户之后,出现此错误:
反应本机TypeError:无法读取未定义的属性“导航”
稍后,我将添加更多详细信息(日志文件等,一旦我学会了如何从本地计算机上对它们进行grep )。
为了更好地理解,我将整个代码放在这里(抱歉,缩进会降低可读性):
const auth = firebase.auth();
const firebaseUser = '';
const usersRef = firebase.firestore().collection('users');
async handleFacebookButton() {
const { type, token, } = await Facebook.logInWithReadPermissionsAsync(FACEBOOK_APP_ID, {
permissions: ['public_profile', 'email']
});
if (type === 'success') {
//Firebase credential is created with the Facebook access token.
const credential = firebase.auth.FacebookAuthProvider.credential(token);
auth.signInAndRetrieveDataWithCredential(credential)
.then(function(userCredential) {
newUserCheck = userCredential.additionalUserInfo.isNewUser;
console.log('newUserCheck = ', newUserCheck)
});
this.setState({loggedIn: "You are signed in"})
this.setState({signedIn: true})
console.log('you are signed in');
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
firebaseUser = {name: user.displayName, uid: user.uid, email: user.email}
console.log(firebaseUser.name, ' and ', firebaseUser.uid);
var existingRef = usersRef.doc(firebaseUser.uid);
existingRef.get().then(function(documentSnapshot) {
// check if user is registered
if(documentSnapshot) {
data = documentSnapshot.data();
console.log('existing user exists!!');
// check if user is an administrator
if (data.administrator == true) {
console.log('existing administrator exists!!');
this.props.navigation.navigate('Admin');
} else { this.props.navigation.navigate('Main');
}
}
});
(error => {
console.log('user not accessed: ', error);
});
//User is not yet in firebase database and needs to be saved
// double check that user is a new user
if (newUserCheck == true) {
this.ref
.doc(uid)
.set({
id: firebaseUser.uid,
username: firebaseUser.name,
email: firebaseUser.email,
})
this.props.navigation.navigate('ChooseRoute')
}
}
})
}
// If login type is not success:
(error => {
this.setState({loggedIn: "Login failed: log in again"})
this.setState({ errorMessage: error.message });
});
}
答案 0 :(得分:0)
我修好了!! 3天后-这是一个有约束力的问题-经过几次失败的尝试,找出了要绑定的函数的正确部分,我将'auth()。onAuthStateChanged和'documentSnapshot'都转换为粗箭头函数,并且错误消失了!!感谢天才ES6 ...!希望这可以帮助其他人...