这是在Firebase云功能中返回ES6承诺的正确方法吗?

问题描述:

我有一个类似于此的云函数:

I have a cloud function similar to this:

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不匹配。通过以下方式发送错误消息的正确方法是什么?

I read this method on a blog somewhere. Now the problem is, when I put wrong OTP on the client side, it shows error as INTERNAL rather than showing the error message OTP did not match. What would be the correct way to send the error message through?

由于 err.message 返回内部 ,那么您需要将返回的错误更改为所需的内容:

Since the err.message is returning Internal, then you need to change the returned error to what you want:

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