在 Vue 中使用 Firestore,.where 不返回任何结果 - 但它们存在

我对 Firestore 很陌生。我有这个查询显示聊天消息和新消息出现时的更新。我只希望当前用户(学校)看到来自他们自己学校的消息。如果我不包含 .where 子句 - 它会显示所有消息,但如果我使用它则不会显示任何内容(即使它们存在)

这是我的 firestore 页面图像的链接(因为我无法嵌入它):

http://img4.mukewang.com/646f0f510001c02506450264.jpg

我在挂载上调用方法:


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;

    })

}

我在这里做错了什么吗?


慕容3067478
浏览 104回答 1
1回答

慕少森

您的过滤器期望它user.schoolId是一个数组:.where("user.schoolId", "array-contains", schoolsIdentification)但它不是一个数组——它只是一个字符串。如果要过滤可能包含多个不同可能值的字符串字段,则应改用“in”查询,如文档中所示。使用in运算符将同一字段上最多 10 个相等 ( ==) 子句与逻辑或组合起来。in 查询返回给定字段与任何比较值匹配的文档。.where("user.schoolId", "in", schoolsIdentification)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript