【问题标题】:need some processing to do before cloud function returns a promise. But promise is return before the processing在云函数返回承诺之前需要进行一些处理。但是承诺是在处理之前返回
【发布时间】: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


【解决方案1】:

我想这就是你要找的东西

export const  addMessage= functions.https.onCall(async (data:any, context) => {

  const col = admin.firestore().collection("joshua");
  var match:[]
  match = data.list  
  var perm1 = []
  // res11.push("454675556") // ? undefined

  for(let val in match){
    var inter = await getValues(col,val)
    console.log("inside resolve" + inter)
  }

  perm1.push("23432") // ??
  // console.log("just before resolve")

  return Promise.resolve(perm1)

});



const getValues = async (col1:any, val1:any) => {
  const query = col1.where('Listed','array-contains', val1)
  var value = await query.get().then(getAllDocs)
  return value
}

const getAllDocs = function(data: any) {
  const temp: Array<any> = []
  data.forEach(function (doc: any) {
    temp.push(doc.data())
  })
  return temp
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2020-12-20
    相关资源
    最近更新 更多