我对 Firestore 很陌生。我有这个查询显示聊天消息和新消息出现时的更新。我只希望当前用户(学校)看到来自他们自己学校的消息。如果我不包含 .where 子句 - 它会显示所有消息,但如果我使用它则不会显示任何内容(即使它们存在)这是我的 firestore 页面图像的链接(因为我无法嵌入它):我在挂载上调用方法:mounted() { //fetches the messages this.fetchMessages(this.$route.params.id);},那么方法是:fetchMessages(schoolsIdentification) { db.collection('chat') .where("user.schoolId", "array-contains", schoolsIdentification) .orderBy('date') .onSnapshot((querySnapshot) => { let allMessages = []; querySnapshot.forEach(doc => { allMessages.push(doc.data()) }) console.log("the all messages array length is:", allMessages.length) this.messages = allMessages; })}我在这里做错了什么吗?
1 回答

慕少森
TA贡献2019条经验 获得超9个赞
您的过滤器期望它user.schoolId
是一个数组:
.where("user.schoolId", "array-contains", schoolsIdentification)
但它不是一个数组——它只是一个字符串。如果要过滤可能包含多个不同可能值的字符串字段,则应改用“in”查询,如文档中所示。
使用
in
运算符将同一字段上最多 10 个相等 (==
) 子句与逻辑或组合起来。in 查询返回给定字段与任何比较值匹配的文档。
.where("user.schoolId", "in", schoolsIdentification)
添加回答
举报
0/150
提交
取消