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
相关产品推荐
相关产品推荐

