【问题标题】:Firestore array-contains-any not working with array value?Firestore 数组包含任何不适用于数组值?
【发布时间】:2020-02-28 10:44:36
【问题描述】:

我有 firestore 集合,但过滤查询没有按预期工作,但是当我从 firebase 数据库 UI 过滤时,它的工作很糟糕。

代码:

const docBookingRef = firestore().collection("booking").where("parentCase", "array-contains-any", [66])
docBookingRef.get().then((doc) => {
    const list = [];
    doc.forEach(function (docV) {
      list.push(docV.data());
    });
    console.log('booking list', list);
  }).catch(function (error) {
    console.log('booking error', error);

  })

【问题讨论】:

  • 老实说,您的查询似乎是正确的,您是否尝试过.where("parentCase", "array-contains", 66) 之类的方法以检查您是否收到该文件?
  • @ralemos 如果与文档中的内容相比,语法看起来是正确的:firebase.google.com/docs/firestore/query-data/…
  • 你的代码没有检查get()返回的promise的错误。可能有错误,你可能不知道。
  • @DougStevenson 代码已更新。但它没有进入捕获功能。显示一个空列表

标签: javascript react-native google-cloud-firestore


【解决方案1】:

我无法在此JSBin 中重现问题。我的集合有一个文档,其中包含一个数组字段 parentCase 和一个值 66

除此之外,代码是你的一个相当直接的副本:

const query = root.where("parentCase", "array-contains-any", [66])
query.get().then((doc) => {
  const list = [];
  doc.forEach(function (docV) {
    list.push(docV.data());
  });
  console.log('booking list', list);
}).catch(function (error) {
  console.log('booking error', error);
})

您确定您的数组包含一个数值,而不是字符串"66"?对于后一种情况,查询必须是:where("parentCase", "array-contains-any", ["66"])

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 2020-12-28
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2019-07-26
    • 2019-03-21
    相关资源
    最近更新 更多