【问题标题】:How to handle promise in firestore如何在firestore中处理promise
【发布时间】:2018-03-26 16:30:24
【问题描述】:

这是我检查doc 是否存在于collections 中的代码

this.shopCollection = this.afs.collection<Shop>('shops', ref => {
  return ref.where('fulladdress', '==', myString)
});

我不知道为什么我不能用这种方法使用.then().catch()。当我将字符串传递给查询时,如果没有找到结果我怎么知道?

我正在使用angularfire2 版本^5.0.0-rc.1angular ^4.2.4firebase ^4.5.0

请帮忙。

【问题讨论】:

  • 你说的是这个方法的使用吗? firebase.google.com/docs/reference/js/…
  • @DougStevenson 是的,因为我使用的是angularfire2,他们称之为AngularFirestoreCollection。看起来两者都是一样的。
  • 返回的 AngularFirestoreCollection 不是一个承诺。查看文档以了解您可以使用它做什么,您有几个选择:github.com/angular/angularfire2/blob/master/docs/firestore/…
  • 它是一个可观察的。而不是.then() 你有.subscribe()
  • .subscribe 在查询返回数据时起作用。但如果没有数据.subscribe 甚至没有运行。 This 是我想要在 angularfire2 中实现的目标

标签: javascript firebase promise angularfire2 google-cloud-firestore


【解决方案1】:

我做了一些改动

这是我的代码

this.afs.collection<Shop>('shops').ref.where('fulladdress', '==', myString)
      .get()
      .then(res => {
        if(res.docs.length == 0){
          //no documents found
        }else{
          //you got some documents
          res.forEach(shop => {
            console.log(shop.id);
            console.log(shop.data());
          })
        }
      }).catch(err => {
        console.log('something went wrong '+ err)
      });

我不确定这是正确的方法,对我来说很好。

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 2021-10-23
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多