javascript - Firebase 函数:orderBy 函数不起作用
问题描述
以下代码在模拟器中工作,但在includeVideos
或includeAudios
时在 firebase 中返回一个空数组false
:
let query = db.collection("content");
if (includeVideos && !includeAudios) {
query = query.where("type", '==', "video").orderBy("type");
}
if (includeAudios && !includeVideos) {
query = query.where("type", '==', "audio").orderBy("type");
}
if (!includeVideos && !includeAudios) {
return {empty json....}
}
if (matchingPersonID != undefined) {
query = query.where("attributionID", '==', matchingPersonID).orderBy("attributionID");
}
//either categories OR matchingSeriesID is always undefined/empty
if (matchingSeriesID != undefined) {
query = query.where("seriesIDs", 'array-contains', matchingSeriesID).orderBy("seriesIDs");
}
if (matchingCategories.length != 0) {
// 'OR' LOGIC
query = query.where("categories", 'array-contains-any', matchingCategories).orderBy("categories");
}
if (lastDocumentID != undefined) {
await db.collection("content").doc(lastDocumentID).get().then((snapshot) => {
query = query.startAfter(snapshot);
log(`starting after document ${snapshot}`)
})
}
//the code works when this line is removed, but the query needs to be sorted by time in order for pagination to work
query = query.orderBy("upload_date", "desc");
return query.limit(getNext).get()...
我在谷歌云日志中没有收到错误消息,所以我没有简单的方法来创建索引,如果这是我需要做的。在 firestore 中,字段upload_date
、attributionID
、type
、seriesIDs
和categories
都在文档中的同一级别。
任何帮助将非常感激!!
解决方案
正如您评论的那样,似乎缺少该字段的索引。
本文档提到了两种处理这些索引的方法:
通过使用 CLI。您可以将索引以 JSON 格式保存到本地文件中。为此,
firebase firestore:indexes
在您的 firebase 项目文件夹中运行,您将获得一个格式化的 JSON 输出供您复制、添加一个新输出,然后使用firebase deploy --only firestore:indexes
.实际上,当 Firebase 捕获使用不存在的索引的错误时,您可以使用由 Firebase 生成的 URL,但您也可以从应用程序的 Firebase 控制台手动执行此操作:
推荐阅读
- data-visualization - 如何使用 Tableau 为赛车条形图创建动态 x 轴
- mysql - 查找日期重叠:如何在 mysql 中查找用户与其他用户重叠的日期?
- c - 我们的教授要求我们编写一个 C 程序,该程序将使用 while 循环显示数字的立方体
- gulp - Gulp 或 grunt 插件来增加一个值?
- r - 按向量 R 的顺序查找先前最大值的列表
- java - 按住按钮 2 秒钟,然后显示 AlertDialog
- javascript - 如何访问反应组件之外的状态/功能
- android - Libs Visual Studio 2019 中的动态链接
- java - Spring Boot REST API 不支持的媒体类型
- java - Apache CXF WS-Security:安全处理失败(操作不匹配)