反應原生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