javascript - 抛出新功能。Firebase 云功能上的 https.HttpsError 被客户端拒绝为内部错误

标签 javascript reactjs firebase google-cloud-functions

我将以下 Cloud Function 部署到我的 Firebase 项目中:

exports.createCredentials = functions.https.onCall((data, context) => {
    if(!context.auth)
        throw new functions.https.HttpsError('failed-auth', 'You must be authenticated to call this function')

    const { email, password } = data;

    if(!(typeof email === 'string'))
        throw new functions.https.HttpsError('invalid-email', 'Email must be a string')
    if(!(typeof password === 'string'))
        throw new functions.https.HttpsError('invalid-password', 'Password must be a string')

    return admin.auth().createUser({
        email,
        password
    })
    .then(userRecord => {
        return { userRecord }
    })
    .catch((error) => { 
        throw new functions.https.HttpsError(error.code, error.message)
    })
})

问题是,每当我抛出一个新的 functions.https.HttpsError 而不是在客户端中捕获的错误为 docs状态,我得到一个 internal 错误代码。在我的函数日志中,它在我尝试调用 HttpsError 的地方显示了一个未处理的错误。这是日志的示例:

Unhandled error Error: Unknown error status: auth/email-already-exists
    at new HttpsError (/user_code/node_modules/firebase-functions/lib/providers/https.js:80:19)
    at admin.auth.createUser.then.catch (/user_code/index.js:26:9)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我知道如果没有抛出 HttpsError,这种类型的函数会拒绝内部错误,但据我所知,在我的函数中情况并非如此。我正在用 React 编写我的前端,所以我是这样调用这个 firebase 函数的:

let options = {
    email: arquitect.email,
    password: arquitect.password
}

let createCredentials = firebase.functions().httpsCallable('createCredentials')
createCredentials(options)
    .then(result => {

    })
    .catch(error => console.log(error))

有什么我想念的吗?

最佳答案

您似乎没有使用值 listed here对于代码参数,根据您链接的文档。该值必须是 Google API 的规范错误代码之一。例如,“failed-auth”或“invalid-email”未在此处列出。您可以改用“permission-denied”或“invalid-argument”。

关于javascript - 抛出新功能。Firebase 云功能上的 https.HttpsError 被客户端拒绝为内部错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50936424/

相关文章:

reactjs - Firestore 按数组的字段值查询

firebase - 在 Unity 上从 Firebase 远程配置获取广告 ID 时出现问题

javascript - 验证多组单选按钮

javascript - 完全自动化传递作为开发 pull 请求确认的一部分

javascript - 如何清除单选按钮/将所有选定的单选按钮清除为错误状态。 Reactjs- Bootstrap

javascript - 输入验证 - react 表单提交时不带任何值

ios - 观察观察者单个事件中的单个事件

javascript - react 更新永远循环 componentDidUpdate

c# - 将日期参数传递给对 MVC 操作的 ajax 调用的安全方法

javascript - videojs的时间格式