我正在测试一个非常简单的实现,如 FB 文档 (https://firebase.google.com/docs/functions/callable) 中所述,但它不起作用。
这是我的 Firebase 函数,部署到云端:
exports.getRecSkills = functions.https.onCall((data, context) => {
return {text: data.text};
});
...和我的 客户来电 (初始化FB后):
var getRecSkills = firebase.functions().httpsCallable('getRecSkills');
getRecSkills({text: '123'}).then(function(result) {
console.log(result);
}).catch(function(error) {
console.log(error.code);
console.log(error.message);
});
我得到一个与 CORS header 相关的问题,但在文档中,它没有提到对 CORS 的需求......我错过了什么吗?
一些注意事项:
数据库),所以我不认为是我错误地设置了 Firebase。
400 错误)通过 shell 进行本地测试时,即使我知道了
与任何其他数据库和 https 函数一起使用
已经为此苦苦挣扎了一段时间...请帮助!
最佳答案
摆脱你的CORS
错误,请确保您的 firebase.json
有以下 headers
:
"hosting": [
{
"headers": [
{
"source": "**",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
}
]
}
]
如果您在 上运行Firebase 模拟器 在本地设备上,确保您在初始化 Firebase 函数后具有以下内容,否则您的本地设备仍将调用远程 Firebase 函数,您将点击 CORS
又报错:if (window.location.hostname === "localhost") {
console.log("localhost detected!");
firebase.functions().useFunctionsEmulator('http://localhost:5001');
};
关于javascript - Firebase 云功能 - onCall 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50195817/