【问题标题】:Iterate all items in Firestore collection by key with pagination使用分页键迭代 Firestore 集合中的所有项目
【发布时间】:2018-06-05 14:07:54
【问题描述】:

我认为这会更容易,但到目前为止还没有发生。我想遍历集合中的所有项目,而不是一次性下载每个项目。

假设我的收藏位于 ref /users 并且每个用户条目都有一个作为用户名的键,这样你最终会得到

/users/
       jonsmith89: {}
       janedoe_: {}
       ...

我没有orderBy() 的字段,因为这些对象中包含嵌入式集合,因此Pagination 的文档对我没有帮助。这就是我目前所拥有的

async IterateCollection(path, dataFunction, limit = 10) {
    let collection = this.db.collection(path)
        .limit(limit);

    let current = collection;
    let entries = 0;

    do {
        let snapshot = await current.get();

        // Do something with snapshot.docs......

        let last = snapshot.docs[snapshot.docs.length - 1];
        current = collection.startAfter(last.data())

        entries = snapshot.size;
    } while (entries > 0);
}

我得到的是一个例外:

指定的游标值过多。指定的值必须与查询的 orderBy() 约束相匹配。

哪种是有意义的,但是我怎样才能按 id(key) 排序呢?我尝试了一个空白的orderBy() 以及orderBy('id'),即使没有 id 字段

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    startAfter 方法需要DocumentSnapshot,而不是data

    startAfter(snapshot: DocumentSnapshot): Query;
    

    所以你必须删除.data()

    current = collection.startAfter(last)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-13
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      相关资源
      最近更新 更多