反应原生TypeError:无法读取undefined的属性'navigation'

[英]react native TypeError: Cannot read property 'navigation' of undefined


I am using the FacebookAuthProvider by firebase to login my users from my platform.

我正在使用firebase的FacebookAuthProvider从我的平台登录我的用户。

I'm using react native in expo with firestore and it was working fine till I tried to add in some checks to redirect users to the correct screens after login. There are two different roles (administrators and users) which have to be separate right after the login.

我正在使用在本地的反应中使用firestore进行反应,并且它正常工作,直到我尝试添加一些检查以在登录后将用户重定向到正确的屏幕。登录后,有两个不同的角色(管理员和用户)必须分开。

if (/* user is administrator */) {
  this.props.navigation.navigate('Admin');
} else {
  this.props.navigation.navigate('Main');
}

After adding this method to separate users by there roles, I got this error:

添加此方法以按角色分隔用户后,我收到此错误:

react native TypeError: Cannot read property 'navigation' of undefined

反应原生TypeError:无法读取undefined的属性'navigation'

Later I will add some more details (log files etc. as soon as I've learned how to grep them from my locale machine).

稍后我将添加更多细节(日志文件等,一旦我学会了如何从我的语言环境机器上grep它们)。

For better understanding I put my whole code here (sorry for the bad indentations which lesses the readability):

为了更好地理解,我把我的整个代码放在这里(对于那些提高可读性的不良缩进感到抱歉):

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

    });
}

1 个解决方案

#1


0  

I fixed it!! 3 days later - it was a binding issue - after several unsuccessful attempts to work out which were the right parts of the functions to bind I converted both 'auth().onAuthStateChanged' and 'documentSnapshot' into fat arrow functions and the errors are gone!! Thank goodness for ES6...! Hope this helps someone else down the line...

我修好了它!! 3天后 - 这是一个绑定问题 - 在几次尝试失败的尝试之后,哪些是绑定函数的正确部分,我将'auth()。onAuthStateChanged'和'documentSnapshot'转换为胖箭头函数,错误消失了!谢天谢地,ES6 ......!希望这有助于其他人下线......


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/08/03/ed2efea5f999c48eecef1ab1d9995c91.html



 
  © 2014-2022 ITdaan.com 联系我们: