【问题标题】:Firebase Cloud Functions – JavaScript/Typescript Promise reject() issueFirebase Cloud Functions – JavaScript/Typescript Promise reject() 问题
【发布时间】:2021-01-09 15:23:51
【问题描述】:

我正在使用 Cloud Functions 方法,例如:

export const someCallableCloudFunction = functions.https.onCall((data, context) => {
   […]
   return new Promise((resolve, reject) => {
      […]
      if (someStatement) {
         resolve("resolveDataString")
      } else {
         reject({ status: "error", code: "429", message: "someErrorMessage" })
      }
   })
})

并用以下方式调用它们:

firebase.functions().httpsCallable("someCallableCloudFunction")(/*some args*/)
   .then(data => { /* data = "resolveDataString" */ })
   .catch(error => { /* here's the problem */ })

问题:这是调用该方法的客户端中的错误:[Error: INTERNAL]。这就是 Firebase Cloud Functions 控制台(模拟器!)中错误日志的样子:

{
  "status": "error",
  "code": "429",
  "message": "Unhandled error",
  "severity": "ERROR"
}

resolve() 工作得很好,但reject() 1) 没有正确打印 Cloud Functions 方法中定义的错误对象,并且 2) 没有将错误对象提供给客户端。相反,客户端会收到[Error: INTERNAL]

我是否必须使用resolve() 来解决和拒绝以及定义可由客户端处理的语句?

【问题讨论】:

    标签: javascript typescript firebase asynchronous google-cloud-functions


    【解决方案1】:

    doc中所述:

    为确保客户端获得有用的错误详细信息,请从 可通过抛出(或返回被拒绝的 Promise)调用 functions.https.HttpsError 的实例。

    如果您的函数抛出 HttpsError 以外的错误,您的 客户端反而收到一个错误带有消息 INTERNAL 和 内部代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2018-12-13
      • 1970-01-01
      • 2018-11-23
      • 2020-07-25
      相关资源
      最近更新 更多