angular – 错误:未捕获(在promise中):TypeError:无法将属性’isAdmin’设置为null

前端之家收集整理的这篇文章主要介绍了angular – 错误:未捕获(在promise中):TypeError:无法将属性’isAdmin’设置为null前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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行遇到错误

Error: Uncaught (in promise): TypeError: Cannot set property ‘isAdmin’ of null

请帮忙!

要么你可以使用箭头功能
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;

否则在调用它时不会指向当前函数.

另见https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions

原文链接:https://www.f2er.com/angularjs/240387.html

猜你在找的Angularjs相关文章