【问题标题】:Using Firestore in Vue, .where doesn't return any results - but they exist在 Vue 中使用 Firestore,.where 不返回任何结果 - 但它们存在
【发布时间】:2020-10-19 13:34:58
【问题描述】:

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

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

我在 mount 上调用方法:

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;
    })
}

我在这里做错了吗?

【问题讨论】:

    标签: javascript vue.js google-cloud-firestore


    【解决方案1】:

    您的过滤器认为 user.schoolId 是一个数组:

    .where("user.schoolId", "array-contains", schoolsIdentification)
    

    但它不是一个数组——它只是一个字符串。如果要过滤可能包含多个不同可能值的字符串字段,则应使用“in”查询,如documentation 所示。

    使用in 运算符可将同一字段上的最多 10 个相等 (==) 子句与逻辑 OR 组合在一起。 in 查询返回给定字段与任何比较值匹配的文档。

    .where("user.schoolId", "in", schoolsIdentification)
    

    【讨论】:

    • 嗯,它现在说:“FirebaseError: [code=invalid-argument]: Invalid Query。'in'过滤器需要一个非空数组。”是否可以只使用 'array-contains' 查询 'user' 数组?
    • 我设法通过将 .where 查询更改为: .where("user.schoolId", "==",schoolIdentification) 来修复它按日期排序)。感谢您的帮助!
    • 如果您发现此答案有帮助,Stack Overflow 上通常会使用上面答案左侧的按钮进行投票并将其标记为正确。
    猜你喜欢
    • 2022-09-14
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2022-01-14
    • 2018-09-17
    • 2017-09-20
    • 1970-01-01
    • 2014-07-21
    相关资源
    最近更新 更多