Firestore 自定义索引不起作用

Son*_*ony 0 firebase google-cloud-firestore

在我的 firestore 数据库中,我有一个名为 的集合states,我已手动添加stateIdstateName到索引中。当我尝试使用云函数访问集合时,它报告崩溃,指出查询需要索引并显示/生成添加索引的链接。但是当我复制粘贴链接时,它没有显示stateName在索引字段中,我基于 进行排序stateName。这是手动索引集合的屏幕截图

在此输入图像描述

这是我获取州列表的代码

exports.getStates = functions.https.onRequest((request, response)=>{
    const db = admin.firEDIT !estore();
    const countryId = request.body['countryId'];
    return db.collection("states").where("countryId","==",countryId).where("isDeleted","==","0")
    .select("stateId","stateName").orderBy("stateName")
    .get().then(snapshot=>{
        const states =[];
        snapshot.docs.forEach(doc=>{
            states.push(doc.data());
        });
        return response.status(200).json(states);
    });
});
Run Code Online (Sandbox Code Playgroud)

这是 firebase 控制台日志中显示的错误

Error: The query requires an index. You can create it here: https://console.firebase.google.com/project/functionstest-54bd9/database/firestore/indexes?create_index=EglkaXN0cmljdHMaDQoJaXNEZWxldGVkEAIaCwoHc3RhdGVJZBACGhAKDGRpc3RyaWN0TmFtZRACGgwKCF9fbmFtZV9fEAI
    at ClientReadableStream._emitStatusIfDone (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:255:19)
    at ClientReadableStream._receiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:233:8)
    at /user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:705:12
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题吗?如何根据stateName工作进行订购?

更新1

我使用了 firebase 控制台日志中提供的链接,然后该链接stateName也列在索引字段中,并且该集合现在已编入索引并且正在按预期工作。这是我的疑问

  1. 为什么第一次不显示stateName,第二次才显示?
  2. 为什么手动索引不起作用?

小智 5

不要手动添加,只需点击链接,它就会登陆 console.firebase。并自动为您创建索引。然后它就会起作用。