javascript - Firestore OrderBy 和Where 冲突

标签 javascript firebase google-cloud-firestore

我从 Firestore 数据库中简单地获取数据,但我想使用一些 orderring 和 where 条件对其进行分页。所以我尝试使用一些基本过滤器获取数据,但在文档 https://firebase.google.com/docs/firestore/query-data/order-limit-data 中遇到错误描述仅对于范围 <、<=、>、>= 应该对同一字段使用 orderBy 和 where,但我只需要完全匹配 (==)

节点 v8.12.0、express、firebase 功能

 model.collection
        .orderBy("dateCreated", 'desc')//timeStamp
        .where('tenantId', '==', 'f8XnOVUKob5jZ29oM9u9')  
        .limit(10)
        .get()
        .then((snapshot) => {
          res.send(snapshot);
        }).catch((error) => res.send(error));

出现下一个错误

{
    "code": "failed-precondition",
    "name": "FirebaseError"
}

仅当单独使用 where 或 orderBy 但不同时使用时才会有结果

最佳答案

使用 compound queries 时,您需要创建一个 index for your queries 。您的查询失败,因为您没有为

创建索引
dateCreated
tenantId

您的索引选项卡应具有类似于以下内容的内容,以及您的索引字段。

index

关于javascript - Firestore OrderBy 和Where 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56614131/

相关文章:

javascript - 多个动态 Canvas 元素?

java - token 未使用 FirebaseInstanceIdService 生成

javascript - Firestore : Get subcollection of document found with where

Firebase:sendSignInLinkToEmail() 抛出: "The given sign-in provider is disabled for this Firebase project."

javascript - 我需要帮助使用 VS-2013 在服务器上部署我的 asp.net 网站

javascript - 对象数组

javascript - 如何在子类中定义 getter 和 setter 属性

javascript - 当同一引用上有多个监听器时,从 Firebase 中删除监听器

firebase - 在使用 firebase 的推送通知中,onTokenRefresh 与 getToken 之间有什么区别?

node.js - 未捕获( promise 中)错误 : FIRESTORE (4. 10.1)内部断言失败:未知关系:数组包含