在我的有角度的firebase项目中,我定义了一个全局变量userexist
。此变量在函数searchUser()
中调用。但是当它被调用时,Error getting document: TypeError: Cannot set property 'userexist' of undefined
会发生错误...您能帮助解决问题吗?
export class AddnewComponent implements OnInit {
userexist: boolean = false;
...
searchUser(value) {
var userId = value.email;
console.log("user ID is:" + userId);
this.userDoc = this.afs.doc('users/'+ userId);
this.afs.doc('users/'+ userId).ref.get().then(function(doc){
if (doc.exists) {
console.log("Document data:", doc.data());
this.userexist = true;
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
}
...