加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 错误:未捕获(在promise中):TypeError:无法将属性

发布时间:2020-12-17 10:16:56 所属栏目:安全 来源:网络整理
导读: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 =
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读