【问题标题】:Is this the correct way of returning a ES6 promise in firebase cloud functions?这是在 Firebase 云函数中返回 ES6 承诺的正确方法吗?
【发布时间】:2020-04-07 14:13:58
【问题描述】:

我有一个类似这样的云功能:

exports.verifyEmail = functions.https.onCall((data, context) => { // data contains session_id (doc id where otp is stored) and otp
  return new Promise((resolve, reject) => {
    admin.firestore().collection('verification').doc(data.session_id).get().then(doc => {
      if(data.otp === doc.data().otp){
        return resolve()
      } else {
        return reject({message: 'OTP did not match'})
      }
    }).catch(err => {
      return reject({message: err.message})
    })
  })
})

我在某处的博客上读到了这种方法。现在的问题是,当我在客户端放置错误的 OTP 时,它显示错误为INTERNAL,而不是显示错误消息OTP did not match。发送错误消息的正确方法是什么?

【问题讨论】:

  • err.message 可能返回内部
  • 是的,客户端的 err.message 给出了字符串 'INTERNAL'。
  • 没有。避免使用Promise constructor antipattern
  • 您是否尝试过使用真正的Error 而不是带有message 的对象?
  • @Bergi ,如何在拒绝时传递错误?请举个例子...

标签: javascript firebase google-cloud-functions es6-promise


【解决方案1】:

由于err.message 正在返回Internal,那么您需要将返回的错误更改为您想要的:

}).catch(err => {
      return reject({message: "OTP did not match"})
    })

【讨论】:

  • .catch 用于从数据库中检索文档时出错。如果没有文件可开始,我无法发送“OTP 不匹配”。然而,当文档存在时,如果 OTP 不匹配,我必须发送“OTP 不匹配”。
  • 如果是进入 catch 而不是 else 那么你还有其他与 otp 无关的问题
猜你喜欢
  • 2021-05-17
  • 2020-08-09
  • 2019-12-17
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2017-11-19
相关资源
最近更新 更多