【问题标题】:How can I combine two firestore queries, "where", in firestoreConnect? [duplicate]如何在 firestoreConnect 中组合两个 Firestore 查询“where”? [复制]
【发布时间】:2019-12-09 08:46:13
【问题描述】:

我正在尝试创建一个查询以获取一个 Firestore 集合/文档,其中 userId 等于当前 userId 或当前 userId 等于contributorsId。

export default compose(
  connect(mapStateToProps),
  firestoreConnect((props) => {
    if (!props.auth.uid) return []
    return [
      {
        collection: 'canvases',
        orderBy: ['createdAt', 'desc'],
        where: [
          ['userId', '==', props.auth.uid] || ['contributors', 'array-contains', props.auth.uid]
        ],
      }
    ]
  })
)(CanvasSelector);

我希望它为我提供所有声明正确的文档。但它只返回第一个。如果我选择只包含其中一个查询(“where”),它们都可以工作并返回正确的文档。

【问题讨论】:

  • Firestore 不支持逻辑 OR 查询。您必须分别执行每个查询,并将它们的结果合并到客户端代码中。

标签: reactjs firebase redux react-redux google-cloud-firestore


【解决方案1】:

我认为您没有正确传递查询 试试这样 -

where: [
      ['userId', '==', props.auth.uid],
      ['contributors', 'array-contains', props.auth.uid]
],

当您将相等运算符 (==) 与范围或数组包含子句(、>= 或数组包含)一起使用时,请确保创建复合索引

在此处查看如何创建一个 https://firebase.google.com/docs/firestore/query-data/indexing

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2021-05-31
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多