Firebase Firestore查询:“错误:9 FAILED_PRECONDITION:该查询需要索引。您可以在此处创建它” [英] Firebase firestore query: "Error: 9 FAILED_PRECONDITION: The query requires an index. You can create it here"

查看:87
本文介绍了Firebase Firestore查询:“错误:9 FAILED_PRECONDITION:该查询需要索引。您可以在此处创建它”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我认为是对名为电子邮件的集合的简单查询:

I have what I thought was a simple query on a collection called 'email':

 const emailQuery = admin
    .firestore()
    .collection('email')
    .where('sendAt', '>=', new Date())
    .where('orderId', '==', doc.orderId)
    .where('brandId', '==', doc.brandId);

我在此主题中收到错误,表示我需要创建一个索引对于此查询。我已经尝试为相关字段创建一对,但是我不确定为什么需要它,因为只订购了一个字段,尽管有这些索引,错误仍然存​​在:

I'm receiving the error in the subject of this post, indicating that I need to create an index for this query. I've tried creating a couple for the relevant fields, but I'm unsure of why it's needed since only one field is ordered--and the error remains despite these indexes:

sendAt -> Ascending, brandId -> Ascending, orderId -> Ascending

sendAt -> Descending, brandId -> Ascending, orderId -> Ascending

我未指定 orderBy 如你看到的。另外, sendAt 是日期,而其他两个是简单的字符串,不需要排序。

I'm not specifying an orderBy as you can see. Also, sendAt is a date, while the other two are simple strings that don't need to be ordered.

我需要什么样的索引,或者如何克服此错误?

What kind index do I need, or how do I get past this error?

谢谢!

推荐答案

来自文档


如果您尝试使用不映射
的range子句进行复合查询到现有索引,您将收到一个错误。错误消息包括
a直接链接,用于在Firebase控制台中创建丢失的索引。

If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

用另一种方式说明:使用比较时涉及< > 的查询在该字段上存在隐式排序。与其他字段的条件结合使用时,特定集合ID需要一个索引。

To state it another way: when you use a comparison that involves < or >, there is an implicit ordering in the query on that field. When used in combination with conditions on other fields, an index is required for the particular collection ID.

您在问题标题中开始引用的错误消息应包含指向以下内容的链接将为该查询创建所需索引的控制台。只需点击该链接并授权创建索引即可。

The error message that you started to quote in the title of your question should contain a link to the console that will create the required index for that query. Just follow that link and authorize the creation of the index.

这篇关于Firebase Firestore查询:“错误:9 FAILED_PRECONDITION:该查询需要索引。您可以在此处创建它”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆