【问题标题】:Error - firestore is not defined (Only after "then") [duplicate]错误-未定义firestore(仅在“then”之后)[重复]
【发布时间】:2019-08-26 20:13:29
【问题描述】:

我有一个问题:当我想在我的数据库中创建假用户时,一切都像魅力一样工作,直到我开始循环“for”。

此时,我有这个错误:

添加文档时出错:TypeError: Cannot read property 'firestore' of 未定义

我尝试了多种方法,但没有任何效果......你有什么想法吗?

谢谢大家!

create_NewUser(data:any){
        this.firestore.collection('Users-test').add({
          name:data.name,
          town:data.town,
          gender:data.gender,
          email:data.email,
          picture:data.picture,
          birthdate:data.birthdate
        })
        .then(function(docRef) {
          // console.log("Document written with ID: ", docRef.id);
          let nbInterest:any = data.interests.length;
          // let nbAvailaibilities:any = data.interests.length;
          for (let index = 0; index < nbInterest; index++) {
            this.firestore.collection('Users-test').doc("/" + docRef.id + "/interests/" + index).add({
              interest:data.interests[index]
            }) 
          }
        })
        .catch(function(error) {
            console.error("Error adding document: ", error);
        });
      }

【问题讨论】:

    标签: angular typescript google-cloud-firestore


    【解决方案1】:

    取自here

    在经典函数表达式中,this 关键字根据调用它的上下文绑定到不同的值。然而,对于箭头函数,这是词法绑定的。这意味着它使用了包含箭头函数的代码中的this。

    这意味着:在您作为参数传递给then this 的函数中没有定义。

    箭头函数

    您可以使用绑定到“周围”this 的箭头函数。

    .then((docRef) => {
        // process this here
    })
    

    this绑定到函数

    您可以将对象显式绑定到在函数内被视为this 的函数

    .then(function(docRef) {
         // process this here
    }.bind(this))
    

    【讨论】:

      【解决方案2】:

      这样做:

      在您的 create_NewUser 中:

      const that = this;
      

      for循环内部使用

      that.firestore
      .collection('Users-test').doc("/" + docRef.id + "/interests/" + index).add({
      ......
      });
      

      这是因为 this 不在 then 的范围内,不可引用。

      完成:

      create_NewUser(data:any){
              const that = this;
              this.firestore.collection('Users-test').add({
                name:data.name,
                town:data.town,
                gender:data.gender,
                email:data.email,
                picture:data.picture,
                birthdate:data.birthdate
              })
              .then(function(docRef) {
                // console.log("Document written with ID: ", docRef.id);
                let nbInterest:any = data.interests.length;
                // let nbAvailaibilities:any = data.interests.length;
                for (let index = 0; index < nbInterest; index++) {
                  that.firestore.collection('Users-test').doc("/" + docRef.id + "/interests/" + index).add({
                    interest:data.interests[index]
                  }) 
                }
              })
              .catch(function(error) {
                  console.error("Error adding document: ", error);
              });
            }
      

      绑定也是scorpioo590建议的一种方式

      【讨论】:

      • 好答案!谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 2018-04-03
      • 2018-09-28
      • 2021-10-09
      • 2014-11-19
      • 2018-04-14
      相关资源
      最近更新 更多