gpt4 book ai didi

angular - 错误 : Uncaught (in promise): TypeError: Cannot set property 'isAdmin' of null

转载 作者:太空狗 更新时间:2023-10-29 17:23:41 24 4
gpt4 key购买 nike

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;

否则 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com