首页 > 解决方案 > 在本地调用 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。

是否可以在本地运行托管和功能以进行开发?

标签: firebasegoogle-cloud-functionsaws-cloud9

解决方案


我有同样的问题,我的解决方案很简单,我用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!" }
})

也许这也是你的问题?


推荐阅读