You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中用react-native-firebase按DocumentReference过滤Firestore数据

你的代码无法正确查询的核心原因是类型不匹配:strategyCounts集合中的strategy字段是DocumentReference类型,但你当前传入where条件的是文档路径字符串,Firestore不会自动匹配两种不同类型的值,因此查询不到结果。

正确实现代码

const subscriber = firestore()
  .collection('strategyCounts')
  .where(
    'strategy',
    'in',
    // 将路径字符串统一转为DocumentReference对象
    user.strategies.map(strategyPath => firestore().doc(strategyPath))
  )
  .onSnapshot(querySnapshot => {
    const resultList = []
    querySnapshot.forEach(doc => {
      resultList.push({
        id: doc.id,
        ...doc.data()
      })
    })
    // 此处写后续处理查询结果的逻辑
  })

注意事项

  • Firestore的in查询单次最多支持传入10个过滤值,如果user.strategies数组长度超过10,需要拆分为多次查询后自行合并结果
  • 注意校验路径字符串的准确性,要和Firestore中实际的集合名称、文档ID完全匹配,避免拼写错误导致生成无效的DocumentReference

内容的提问来源于stack exchange,提问作者Merim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 21:15:06