firebase.auth().onAuthStateChanged((user) => {
  if(user) {
    this.isLoggedIn = true; //Set user loggedIn is true;
    this.isAdmin = false;
    firebase.database().ref('/userProfile/' + user.uid).once('value').then(function(snapshot) {
      let userInfo = snapshot.val();
      if(userInfo.isAdmin == true) {
        //ERROR AT THIS LINE:
        //Error: Uncaught (in promise): TypeError: Cannot set property 'isAdmin' of null
        this.isAdmin = true;
        console.log(userInfo);
      }
    });
  } else {
    this.isLoggedIn = false; //Set user loggedIn is false;
  }
});

我在第8行出现错误

最佳答案

您可以使用箭头功能

firebase.database().ref('/userProfile/' + user.uid).once('value')
.then((snapshot) => {

或使用
var self = this;

firebase.database().ref('/userProfile/' + user.uid).once('value')
.then(function(snapshot) {
   self.isAdmin = true;

否则this在被调用时不会指向当前函数。

另请参阅https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions

关于angular - 错误: Uncaught (in promise): TypeError: Cannot set property 'isAdmin' of null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38807643/

10-10 21:59