【发布时间】:2023-03-15 12:36:02
【问题描述】:
我有一个云函数,我在其中传递一个数字数组并将这些数字与 firestore 中的集合进行比较。如果数字存在,则返回包含这些数字的数组。但在比较这些数字之前,函数会在 promise 中返回空值。
我尝试过使用异步等待,但执行顺序保持不变。
//排序联系人列表
export const addMessage= functions.https.onCall(async (data:any, context) => {
const col=admin.firestore().collection("joshua");
var match:[]
match=data.list
var perm1=new Array()
res11.push("454675556")
console.log("above resolve")
for(let val in match){
var inter=await Promise.all([getValues(col,val)])
console.log("inside resolve"+inter)
}
perm1.push("23432")
console.log("just before resolve")
return new Promise((res,rej)=>{
res(perm1)
})
});
//the async function which is suppose to process on every iteration
function getValues(col1:any,val1:any)
{
return new Promise(async(res,rej)=>{
var query= await col1.where('Listed','array-contains',val1)
var value=await query.get()
res(value)
})
.catch(err=>{
console.log(err)
})
}
我希望序列是异步的,其中等待 getValues 的返回值,并等待 query.get 的 getValues 结果。 这样最终返回仅在所有过程完成后才发送。
【问题讨论】:
-
您可能忘记了
async声明getValues。但我没有深入研究你的代码
标签: android node.js firebase google-cloud-firestore google-cloud-functions