Firebase 使用云函数从firestore中的复合查询获取数据?

Firebase 使用云函数从firestore中的复合查询获取数据?,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,首先,我用一个文档搜索一个特定的文档。在这个函数中,我使用不同的过滤器从数据中获取特定的值。这个过滤器的结果是一个数组,它将使用它形成一个foreach。结果在elementosaux中串联,next等于elementos。问题是我打印以获取值元素,结果未定义 这是代码。谢谢你的帮助 exports.autentifyday=functions.https.onRequestreq,res=>{ corsreq,res,=>{ var db=admin.firestore; const key=

首先,我用一个文档搜索一个特定的文档。在这个函数中,我使用不同的过滤器从数据中获取特定的值。这个过滤器的结果是一个数组,它将使用它形成一个foreach。结果在elementosaux中串联,next等于elementos。问题是我打印以获取值元素,结果未定义

这是代码。谢谢你的帮助

exports.autentifyday=functions.https.onRequestreq,res=>{ corsreq,res,=>{ var db=admin.firestore; const key=req.query.key; const dia=req.query.dia; db.collection/servicios\u disposibles/.dockey.get.thenfunctiondoc{ var-Ddr; var-Str; var horario; var horas; 贺拉索变种; 选择了var Horasse; var elementos:任意[]=[]; var elementosaux:any[]=[]; var元素; 如果存在文档{ 他当选了; Ddr=JSON.parseJSON.stringifydoc.data; horario=Ddr.horario; horario.filterdias:{dia:any;disponibilidad:boolean;}=>{ 如果直径=直径{ ifdias.disponibilidad==true{ dias=dias; } } }; horas=JSON.parseJSON.stringify; horasaux=horas.horas; horasselected=horasaux.filterhora:{disponibilidad:boolean;}=>hora.disponibilidad==true; horasselected.forEachfunctionhora:{hora:any;}{ db.collectionpedidos_servicios.wherekey,==,key .wherehoras,数组包含,hora.hora.get.ThenSapshot=>{ snapshot.forEachdoc3=>{ elementoselement={ horas:doc3.data.horas } elementosaux=elementosaux.concatelementoselement; console.logelementosaux//return带有应答查询的数组 }; elementos=[]; elementos=elementosaux; console.logelementos//在这一点上没有定义 }.catch=>console.loge } } console.logelementosaux//在这一点上没有定义 res.sendStr; 返回Str; }.catchreason=>{ 第二项理由; 原因; 返回原因; }; }; };
这可能是一种异步代码。我参与了您的代码并创建了一个简单的示例:

const db = new Firestore()

var elementos: any[] = [];
var elementosaux: any[] = [];
var elementoselement;

let colRef = db.collection("collection1");

if (true) {
    console.log("start")
    colRef.get().then((snapshot) => {
        snapshot.forEach(doc3=>{
            elementoselement = {
            "horas": doc3.data().horas
            }
            console.log("elementoselement",elementoselement)
            elementosaux = elementosaux.concat(elementoselement);
            console.log("elementosaux inside snapshot", elementosaux)//return an array with the answer query
        });
        elementos=[];
        elementos=elementosaux;
        console.log("elementos inside snapshot", elementos)//is undefined in this point
        });
    console.log("elementosaux outside snapshot", elementosaux)
    console.log("elementos outside snapshot", elementos)
}
console.log("elementosaux outside if", elementosaux)//is undefined in this point
console.log("end")
我在firestore集合1中添加了一个包含字段horas:horas_value的文档,以使其正常工作

该示例包含有关特定日志的代码部分的更多日志信息。当您像运行ts node test_main.ts一样运行它时,您会得到以下结果:

start
elementosaux outside snapshot []
elementos outside snapshot []
elementosaux outside if []
end
elementoselement { horas: 'horas_value' }
elementosaux inside snapshot [ { horas: 'horas_value' } ]
elementos inside snapshot [ { horas: 'horas_value' } ]
由于get是异步的,您可以看到它内部的日志位于末尾。 处理这个问题有很多可能性。例如,您可以将整个if放入'async'函数中,并添加'await'以获取:

async function main()  {
    if (true) {
        ....
        await colRef.get().then((snapshot) => {
        ....
}
main()
结果是:

start
elementoselement { horas: 'horas_value' }
elementosaux inside snapshot [ { horas: 'horas_value' } ]
elementos inside snapshot [ { horas: 'horas_value' } ]
elementosaux outside snapshot [ { horas: 'horas_value' } ]
elementos outside snapshot [ { horas: 'horas_value' } ]
elementosaux outside if [ { horas: 'horas_value' } ]
end
只是我不知道你为什么说这是未定义的,它不应该是。。。 我有空数组。。。也许这是一个如何运行代码的问题


我希望这会有帮助

您是否检查过快照中是否有任何内容?如何打印此elementos值?问题是,当我打印值时,它在foreach内部工作,但当我在foreach外部打印时,它显示为未定义,如何让它在foreach完成执行时打印值?问题是我创建了de foreach以进行多次搜索,在最后的i/a示例中,应该可以使用async/await查看所有结果。这是一个很常见的问题,你可以用谷歌搜索如何在同步代码中使用异步函数。例如,我发现了一些可以帮助你的东西。。。