本文介绍了错误:未捕获(承诺):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行出现错误

推荐答案

可以使用箭头功能

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在被调用时不会指向当前函数.

otherwise this doesn't point to the current function when it's called.

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

这篇关于错误:未捕获(承诺):TypeError:无法将属性"isAdmin"设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:35