【问题标题】:getting error while querying in firebase firestore [duplicate]在firebase firestore中查询时出错[重复]
【发布时间】:2020-09-01 17:46:28
【问题描述】:

我收到一条错误消息,提示查询需要一个索引

这是我运行查询后遇到的错误

Error: FirebaseError: The query requires an index. You can create it here:
https://console.firebase.google.com/v1/r/project/jssr-6e16c/firestore/indexes?create_compo
site=Ckhwcm9qZWN0cy9qc3NyLTZlMTZjL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9BZG1pb
i9pbmRleGVzL18QARoLCgdwaG9uZU5vEAEaDQoJcGFydHlOYW1lEAIaDAoIX19uYW1lX18QAg
(failed-precondition)
    at Object.throw_ [as throw] (http://localhost:58377/dart_sdk.js:4776:11)
    at handleThenable
    (http://localhost:58377/packages/firebase/src/storage.dart.lib.js:3237:21)
    at handleThenable.throw (<anonymous>)
    at onError (http://localhost:58377/dart_sdk.js:35663:38)
    at _RootZone.runBinary (http://localhost:58377/dart_sdk.js:35547:58)
    at _FutureListener.thenAwait.handleError (http://localhost:58377/dart_sdk.js:30969:50)
    at handleError (http://localhost:58377/dart_sdk.js:31482:51)
    at Function._propagateToListeners (http://localhost:58377/dart_sdk.js:31505:17)
    at _Future.new.[_completeError] (http://localhost:58377/dart_sdk.js:31366:23)
    at async._AsyncCallbackEntry.new.callback
    (http://localhost:58377/dart_sdk.js:31401:31)
    at Object._microtaskLoop (http://localhost:58377/dart_sdk.js:35759:13)
    at _startMicrotaskLoop (http://localhost:58377/dart_sdk.js:35765:13)
    at http://localhost:58377/dart_sdk.js:31707:9Application finished.

查询生成错误如下。 在此 phoneNo 和 lrNo 都是 document 中的字段,但它们是 String 但只包含数值(在客户端使用正则表达式验证)。

    var temp = await fireStore
        .collection("Admin")
        .where("phoneNo", isEqualTo: TextFieldData.phoneNo)
        .orderBy("lrNo", descending: false)
        .getDocuments()
        .then((value) => value);

    return temp;

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    当使用多个字段进行查询时,您需要创建索引。在您的代码中,您使用phoneNo 进行过滤并使用lrNo 进行排序,因此您可以使用错误中的url 创建索引。

    错误:FirebaseError:查询需要索引。你可以在这里创建它: https://console.firebase.google.com/v1/r/project/jssr-6e16c/firestore/indexes?create_compo 站点=Ckhwcm9qZWN0cy9qc3NyLTZlMTZjL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9BZG1pb i9pbmRleGVzL18QARoLCgdwaG9uZU5vEAEaDQoJcGFydHlOYW1lEAIaDAoIX19uYW1lX18QAg

    只需点击 url,它就会导航到页面并创建索引。

    【讨论】:

    • 执行此操作后,我没有使用相同的查询获得任何数据
    • 我添加了一张图片,请检查我的数据布局。在上面的图片中,document-id 和 lrNo 都相同。
    • 与此无关,请再次阅读答案。在查询中您使用两个字段,您所要做的就是单击链接并创建一个非常简单的索引
    • 当你在查询中使用单个字段时,firestore 会自动为你创建一个索引。索引用于提高性能以更快地检索数据,但它们不会自动为复合查询创建索引
    • 我已经这样做了,在这样做之后我没有得到任何数据,我更早地得到数据(没有按查询排序)。但以一种无序的方式。
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    相关资源
    最近更新 更多