Firebase可调用函数+ CORS [英] Firebase Callable Function + CORS
问题描述
我正在尝试从我的应用程序调用可调用的Cloud Function,但是我遇到了CORS问题.
I'm trying to call a callable Cloud Function from my app, but I'm facing CORS issues.
我无法启用Cors,因为我无法访问onCall函数上的请求和响应.这是我的功能:
I can't enable Cors since I don't have access to the request and response on the onCall function. This is my function:
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 package.json:
Firebase-functions package.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
WEB SDK Firebase version: 4.13.1
推荐答案
对于其他到达此处搜索firebase可调用函数cors错误的人,这是我的清单:
For anybody else who has arrived here searching firebase callable functions cors errors, here's my checklist:
- 确保功能已部署.
- 确保函数名称正确.我应该在
recalculatY
时调用recalculatY
.由于某种原因出现了cors错误. - 确保功能代码本身没有引发错误. 使用仿真器来提供帮助.这仍然没有引发cors错误有助于了解.
- 确保您的区域匹配-我正在使用
europe-west2
.我必须同时在该区域部署该函数,并使用该区域进行调用.有一阵子,我假设如果函数名称正确,客户端将推断出正确的区域.事实并非如此.
- Ensure the function is deployed.
- Ensure the function name is correct. I was calling
recalculatY
when it should have beenrecalculateY
. Got a cors error for some reason. - Ensure the function code itself is not throwing an error. Use the emulator to help. This didn't throw a cors error still helpful to know.
- Ensure your regions match - I am using
europe-west2
. I had to both deploy the function with that region, and call it using the region. For a while, I assumed the client would infer the correct region if the function name was correct. That was not the case.
将可调用函数部署到特定区域:
Deploying a callable function to a specific region:
// This is the file in which you define your callable function.
const functions = require('firebase-functions');
...
exports.yourFunc = functions.region('europe-west2').https.onCall(async (data, context) => {
...
})
从客户端(在本例中为vuejs Web应用程序)的特定区域调用函数:
Calling a function in a specific region from the client (in this case, a vuejs web app):
// In my case, this is a vuex store file, but it is safe to assume this is plain old javascript
import firebase from 'firebase/app'
import 'firebase/functions'
...
firebase.app().functions('europe-west2').httpsCallable('yourFunc')
注意:firebase.app().function...
与firebase.app.function...
这篇关于Firebase可调用函数+ CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!