【问题标题】:Firebase Functions: orderBy function not workingFirebase 函数:orderBy 函数不起作用
【发布时间】:2021-12-17 06:58:30
【问题描述】:

以下代码在模拟器中有效,但当 includeVideosincludeAudiosfalse 时,在 firebase 中返回一个空数组:

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_dateattributionIDtypeseriesIDscategories 字段在文档中都处于同一级别。

任何帮助将不胜感激!

【问题讨论】:

  • matchingPersonIDmatchingCategories 同时给出时,您应该抛出自己的错误,因为同时使用两者会导致 Firebase 出错,但从您处理的方式来看并不明显当两者都给出时。
  • 如果可以的话,还可以考虑使用模块化 Firebase V9 语法,因为每个约束都可以添加到数组中,然后一次性合并到查询中。这使您可以避免在每隔一行重新定义查询并将其切换为使用const
  • 我确实抛出了一个错误,但我没有发布该代码。不过谢谢!
  • 我试图弄清楚如何升级到 v9,但我似乎无法弄清楚。这是否意味着我需要重写所有函数?
  • 另外,我刚刚注意到typeattributionID 上的orderBy 会抛出错误,因为您将它们与等式一起使用。关于升级到v9,可以通过导入firebase/compat/appfirebase/compat/firestore等方式导入兼容库逐步迁移代码。然后当你想使用现代语法时导入firebase/firestore等等。

标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

正如您评论的那样,该字段的索引似乎丢失了。

This documentation 提到了两种处理这些索引的方法:

  • 通过使用 CLI。您可以将索引以 JSON 格式保存到本地文件中。为此,在您的 firebase 项目文件夹中运行 firebase firestore:indexes,您将获得一个格式化的 JSON 输出供您复制、添加一个新输出,然后使用 firebase deploy --only firestore:indexes 部署它们。

  • 确实,当 Firebase 捕获使用不存在的索引的错误时,您可以使用由 Firebase 生成的 URL,但您也可以从应用程序的 Firebase 控制台手动执行:

【讨论】:

  • 我试过了,但我不知道要输入哪些字段或输入的顺序。我无法让它工作。你确定有可能吗?
  • @DavidReese 您需要为查询使用的每个组合创建索引,按照它们添加到查询的顺序。分号分隔的它们是:seriesID:asc, upload_date:desc; categories:array, upload_date:desc; attributionID:asc, upload_date:desc; attributionID:asc, seriesID:asc, upload_date:desc; attributionID:asc, categories:array, upload_date:desc; type:asc, attributionID:asc, seriesID:asc, upload_date:desc; type:asc, seriesID:asc, upload_date:desc; type:asc, attributionID:asc, upload_date:desc; type:asc, attributionID:asc, categories:array, upload_date:desc
  • 即使使用我的代码行 query = query.orderBy("upload_date", "desc") 也能正常工作吗?因为似乎无论我尝试多少索引,这一行都会删除所有数据。
  • 另外,将主要排序机制设为upload_date 是否有效?我想按时间排序。
猜你喜欢
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 2019-03-09
  • 2022-09-28
  • 1970-01-01
相关资源
最近更新 更多