如何在.then函数或查询中更改/保存值

时间:2019-06-13 21:26:29

标签: angular typescript

输入查询时会给出true / false,但是无法将true / false值保存到全局变量

我已经研究/研究了回调函数,但是在任何示例中都提供了,这些函数是分离的,并且似乎独立于它们自己工作

//outside variable
 public registro:any;

public existe(){
//making the consult
this.db.collection(this.year).doc(this.mesActual).collection(this.semana).doc(this.user.displayName).collection(this.diaMo).where("semana","==", this.semaMo).get()
        .then(doc => {
          if(doc.empty){
            // no data found!
            this.registro=false;   //<== happends to be undefined
          }
          else{
            // data found!
            this.registro=true;    //<== so as here, undefined
          }
        })
}

可变注册表总是“未定义”

1 个答案:

答案 0 :(得分:-1)

您遇到范围问题。也许简化最适合您:

ngOnInit(){
    this.registro = this.db.collection...get()
        .then(doc => {
          if(doc.empty){
            return false;   //<== happends to be undefined
          }
          else{
            return true;    //<== so as here, undefined
          }
        })
    }
}

我也投票赞成你。