【问题标题】:Function returned undefined expecting Promise or value using async and await函数返回未定义的期望 Promise 或使用 async 和 await 的值
【发布时间】:2020-06-07 06:40:26
【问题描述】:

我一直在将这个代码结构与 httprequest 云函数一起使用。它适用于那些 httprequest 函数,但现在我得到“函数返回未定义的预期承诺或值”错误,即使我返回“成功!?”。

你知道我哪里错了吗?我是 onWrite 触发器和整个云函数世界的新手...

这是我的代码:

export const toDashboardInfo = functions.firestore.document('maps/{mapId}').onWrite((change, context) => {  

  const doWork = async function()
  {
    //do stuff here with await
    return("Success!?");
  }

  doWork().then((returnData) => 
  {         
    return(returnData);
  }).catch((error) => { return(error)})

});

谢谢!

【问题讨论】:

    标签: typescript firebase async-await google-cloud-functions


    【解决方案1】:

    添加退货:

    return doWork().then((returnData) => 
      {         
        return(returnData);
      }).catch((error) => { return(error)})
    

    【讨论】:

    • 但是请注意,如果执行return returnData;(请参阅here 以获得一些解释)在技术上是有效的,则没有兴趣这样做,因为云功能不会考虑它(即云函数不会“处理”它,因为它可以被 HTTPS 云函数处理,例如发送回前端)。所以简单地做return doWork().catch((error) => {console.log(error); return null;}) 是等价的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多