I have a query for firebase that has an array of IDs that has a size > 10. Firebase has restrictions on the number of records to query in one session. Is there a way to query against more than 10 at a time?

[未经处理的promise 拒绝:FirebaseError:无效查询.'in'筛选器在值数组中最多支持10个元素.]

https://cloud.google.com/firestore/docs/query-data/queries

enter image description here

  let query = config.db
    .collection(USER_COLLECTION_NAME)
    .where("id", "in", matchesIdArray);
  const users = await query.get();

(matchesIdArray.length需要不受限制)

推荐答案

我发现这对我来说很好,不需要做那么多的查询(10次循环和请求).

export async function getContentById(ids, path) {
  // don't run if there aren't any ids or a path for the collection
  if (!ids || !ids.length || !path) return [];

  const collectionPath = db.collection(path);
  const batches = [];

  while (ids.length) {
    // firestore limits batches to 10
    const batch = ids.splice(0, 10);

    // add the batch request to to a queue
    batches.push(
      collectionPath
        .where(
          firebase.firestore.FieldPath.documentId(),
          'in',
          [...batch]
        )
        .get()
        .then(results => results.docs.map(result => ({ /* id: result.id, */ ...result.data() }) ))
    )
  }

  // after all of the data is fetched, return it
  return Promise.all(batches)
    .then(content => content.flat());
}

React-native相关问答推荐

react 本机函数不通过usContext触发

如何在Reaction Native中将身体分成三部分

如何从 onCacheEntryAdded 生命周期回调向 RTK 查询 useQuery() 挂钩抛出错误

TypeError:未定义不是对象(判断'theme.spacing [radius]')

react-native 根据状态隐藏显示按钮

useState 函数似乎阻止了 Animated.timing 事件?

如何在 React Navigation 5 中将父函数传递给子屏幕?

react-native 签名 [AppName]Tests 需要开发团队 ios

用hooks钩子react context上下文防止重新渲染

redux-observable Promise 在单元测试中没有得到解决

React Native - 用分机拨打电话号码

使用 Jest 测试 React Native 应用程序

通过 ToolbarAndroid => onIconClicked 显示 DrawerLayoutAndroid

如何在 React 中使用带有钩子的生命周期方法?

iOS 模拟器如何确认alert信息

如何在 React Native 中使用 Jest

是否需要使用 StyleSheet.create?

通过 IOS 中的 Linking.openURL React Native Open 设置

React Context:TypeError:render is not a function

xcode 使用错误的 node.js 版本