Firestore集合Map字段查询无结果,请求技术协助
排查Firestore Map数组字段查询无结果的问题
我来帮你梳理下代码里的问题,应该就能解决查询不到结果的情况啦:
问题1:操作符多了不必要的空格
你写的'in '后面多了个空格,Firestore的查询操作符是严格匹配的,必须写成'in'才行。
问题2:in操作符的参数类型错误
in操作符要求传入数组作为参数,如果你只是想匹配单个文档引用,直接用==操作符就足够了;只有当你要匹配多个文档引用时,才需要用in加数组。
问题3:确认字段结构与数据类型
要确保myCollection里的文档确实符合这个结构:
- 存在
testMap这个Map类型字段 testMap下有array1数组字段array1数组中存储的是文档引用类型(不是文档ID字符串)
修正后的代码示例
场景1:匹配单个文档引用
const myRef = firebase.firestore().collection('otherCollection').doc('123456'); const res = await firebase .firestore() .collection('myCollection') .where('testMap.array1', '==', myRef) .get();
场景2:匹配多个文档引用(使用in)
const myRef = firebase.firestore().collection('otherCollection').doc('123456'); const anotherRef = firebase.firestore().collection('otherCollection').doc('789012'); const res = await firebase .firestore() .collection('myCollection') .where('testMap.array1', 'in', [myRef, anotherRef]) .get();
额外提示
如果还是查不到结果,可以检查下Firestore的安全规则,确认当前用户有读取myCollection和otherCollection的权限,权限限制也可能导致查询返回空结果哦。
内容的提问来源于stack exchange,提问作者Nagesh
相关产品推荐
相关产品推荐

