javascript - Firebase Firestore查询超过10个元素的数组

标签 javascript firebase google-cloud-firestore

[**enter image description here** enter image description here

我尝试使用用户设置查询后集合,但设置是一个包含 10 多个元素的数组,并且没有返回任何内容。我知道文档确实提到了 10 个元素的限制,有人知道解决方法吗?

firebaseApp.collection('posts')
            .where("newTag", "in", mySettings)
            .get()
let array = [];
        posts.forEach((post) => {
            array.push(post.data());
        });

dispatch({ type: ActionTypes.GET_POSTS, payload: array });

最佳答案

一个简单的数组分块函数可以解决您的问题:

const chunkArray = (list: any[], chunk: number): any[][] => {
    const result = [];

    for (let i = 0; i < list.length; i += chunk) {
        result.push(list.slice(i, i + chunk));
    }

    return result;
};

export { chunkArray };

然后使用 for wait 黑客来获取快照也可以工作:

  const snaps_collection: FirebaseFirestore.QuerySnapshot[] = [];

  for await (const snap of chunks.map(
    async (chunk) =>
      await database
        .collection("collection_name")
        .where("id", "in", chunk)
        .get()
  )) {
    snaps_collection.push(snap);
  }

关于javascript - Firebase Firestore查询超过10个元素的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59257753/

相关文章:

javascript - Underscore 的 findWhere 和 MongoDB 对象

javascript - 使用键或索引方法从 Firebase 服务器删除数据

swift - 将图像保存到 Cloud Firestore

javascript - AngularJS - 从 .NET Controller 生成 View 只执行一次

javascript - 框架 7 - 组合位于不同 html 文件中的所有 html 站点

firebase - Firebase 动态链接是否有使用配额?

javascript - Angular/AngularFire2 Firestore - 初始化可观察集合

firebase - Firestore 使用文档引用获取字段值

javascript - 翻译 OpenLayers map ,例如英语到德语

android - 有没有办法从 FirestoreRecyclerAdapter 获取项目的文档 ID?