firebase - 在本地调用 Firebase 云函数
问题描述
我知道这个问题已经被问过很多次了,我已经阅读了所有的答案,但仍然没有一个可行的解决方案。
我尝试使用此处和此处描述firebase serve
的或在本地运行我的 firebase 功能和托管。firebase emulators:start
这给了我类似的东西:
✔ functions: Using node@8 from host.
✔ functions: Emulator started at http://127.0.0.1:8081
i functions: Watching "/home/ec2-user/environment/functions" for Cloud Functions...
i hosting: Serving hosting files from: build
✔ hosting: Local server: http://127.0.0.1:8080
⚠ functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to XXXXX-d5435114eb69.json. Non-emulated services will access production using these credentials. Be careful!
✔ functions[log]: http function initialized (http://127.0.0.1:8081/XXXXX/us-central1/log).
托管服务器完美运行,并且有一个log
云功能在http://127.0.0.1:8081/XXXXX/us-central1/log
. 但是运行:
const log = firebase.functions().httpsCallable('log');
log("Hello World");
从本地托管站点尝试调用已部署的云功能并给我一个 CORS 错误:
CORS 策略已阻止访问从源“ http://127.0.0.1:8080 ”获取“ https://us-central1-XXXXX.cloudfunctions.net/log ”
我正在使用firebase 自动配置来加载 sdk。
是否可以在本地运行托管和功能以进行开发?
解决方案
我有同样的问题,我的解决方案很简单,我用https.onRequest
的是https.onCall
:
不工作:
// Cloud Function
exports.testFunction = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
})
在职的:
// Cloud Function
exports.testFunction = functions.https.onCall(() => {
return { message: "Hello from Firebase!" }
})
也许这也是你的问题?
推荐阅读
- javascript - 取消导航时如何防止后退和前进浏览器按钮导致页面重新加载和丢弃状态。反应路由器 v4.3
- gcc - 仍然搜索本地文件目录的 g++/gcc 编译的 -I 等效项是什么?
- javascript - 无法使用 discord.js 编辑角色
- python - Python中的Gelu激活
- rx-java2 - 如何将每个可观察到的发射收集到 rxJava2/rxKotlin2 中的一个对象中?
- ruby-on-rails - 使用它所属模型的属性在 has_many 关联中设置模型属性的最全面方法是什么?
- sql - Bigquery SQL 中的并发会话
- python - C++ - 如何链接 python 库?
- python - 如何获得熊猫行的最大精度?
- jsf - 如何将 primefaces 4.0 升级到 6.2