javascript - 在 Firebase Cloud Function 上抛出新函数。 https.HttpsError 被拒绝为客户端上的内部错误
问题描述
我将以下 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
的而不是在客户端中捕获的错误作为文档状态时,我都会收到一个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))
有什么我想念的吗?
解决方案
根据您链接的文档,您似乎没有使用此处列出的代码参数值。该值必须是 Google API 的规范错误代码之一。例如,“failed-auth”或“invalid-email”未在此处列出。您可以改用“permission-denied”或“invalid-argument”。
推荐阅读
- css - 在 Firefox 中勾勒出圆形按钮的轮廓,而不是 Edge
- azure-cognitive-services - 上传 Business Central Base App.xlf 导致“无法提取上传文件的内容”。
- c# - 如何从 C# 获取我在 Linux 上创建的文件夹列表?
- mysql - 加入后MySql和DISTINCT
- html - 在 vb.net 中动态创建的文本框中设置宽度的问题
- node.js - 在JS文件nodejs中导入TS文件找不到模块
- mysql - 将两列的mysql数值group_concat与连接进行比较
- sql-server - 只有在所有表都更新后才需要显示更新的数据
- javascript - 如何使用移动应用程序的相机检测图像到文本
- reactjs - ReactJS:从数据列表中获取 ID