【发布时间】:2018-10-21 01:22:27
【问题描述】:
我正在尝试从我的应用中调用可调用的云函数,但我遇到了 CORS 问题。
我无法启用 Cors,因为我无权访问 onCall 函数的请求和响应。这是我的功能:
exports.UserInvitation = functions.https.onCall((data, context) => {
const email = data.email
return new Promise((resolve, reject) => {
admin.auth().createUser({
email: email,
emailVerified: false,
password: password
}).then(resolve).catch((err) => {
console.error(err.code)
reject(new functions.https.HttpsError(err.code, err.message))
})
})
})
这就是我所说的:
functions.httpsCallable('UserInvitation')({ email: this.input.value }).then((data) => {
console.log('Sent invitation:', data)
})
Firebase-functions 包.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"bluebird": "^3.5.1",
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1"
},
"private": true
}
WEB SDK Firebase 版本:4.13.1
【问题讨论】:
-
CORS 应该由 onCall 处理程序自动处理。我怀疑有关 CORS 的错误消息不准确,是函数返回 500 internal 的结果。您在 Firebase 控制台的函数日志中看到了什么?
-
你说得对,问题出在我的错误处理程序上。文档说我可以拒绝带有functions.https.HttpsError实例的承诺,但即使我这样做了,我也会在客户端收到500错误。我该如何解决?
-
即使我删除了我的 return 语句并添加了
throw new functions.https.HttpsError('test/code', 'error message'),它也只会返回一个带有消息且状态等于“INTERNAL”的对象 -
啊,是的,传递给 HttpsError 的状态码不能是
test/code。它必须是此处列出的标准代码之一:firebase.google.com/docs/reference/functions/… -
@bklimt 请用这个评论创建一个答案,我会接受它:) 我相信这个细节应该添加到 firebase onCall 文档中
标签: javascript firebase google-cloud-functions