获取文档时出错:TypeError:无法设置未定义的属性“ userexist”

时间:2018-08-04 23:59:07

标签: javascript angular firebase

在我的有角度的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);
      });
  }

...

0 个答案:

没有答案