【问题标题】:Checking if the document field exists检查文档字段是否存在
【发布时间】:2019-03-04 20:26:14
【问题描述】:

我正在做一个登录系统,我正在查询用户的条目以验证它是否存在于 firestore,如果它找到他的输入,那么我将它登录到控制台。在这部分一切正常之前,当他输入存在的东西时,它会带来他的数据。但是,当他输入不存在的东西时,它会输入一个空数组“[]”,这意味着不存在这样的东西。我的问题很简单,当他的输入不存在时,我找不到合适的逻辑来控制台日志,仅此而已。

这是我的代码

  pageLogin() {
    var auxint = 0;
        this.dataAux
        let auxString = '[';
    var query = firebase.firestore().collection("armazenaTest")
    query.where('Documento.login', '==', this.User.login).get().then(res => {
      res.forEach(item => {
        if (item.exists) {
          auxint++;
          auxString += '{"id":"' + item.id + '","armazenaTest":' + JSON.stringify(item.data()) + '}';
          if(item.get('Documento.login') == this.User.login){
            console.log('Document found!: ', item.data())
          }
        }
        if (res.size != auxint)
          auxString += ', ';
      })
      auxString += ']';
      this.dataJSON = JSON.parse(auxString);
      console.log(this.dataJSON);
    }).catch(err => {
      console.log('An error occured ' + err);
    });

  }

【问题讨论】:

    标签: angular typescript ionic-framework google-cloud-firestore


    【解决方案1】:

    它应该像在迭代之前检查结果一样简单:

    query.where('Documento.login', '==', this.User.login).get().then(res => {
          if (res.length < 1) {
             console.log('no results!');
             //probably break or return here
          }
          res.forEach(item => {
            if (item.exists) {
              auxint++;
              auxString += '{"id":"' + item.id + '","armazenaTest":' + JSON.stringify(item.data()) + '}';
              if(item.get('Documento.login') == this.User.login){
                console.log('Document found!: ', item.data())
              }
            }
    

    【讨论】:

    猜你喜欢
    • 2023-03-07
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2012-01-08
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多