在本地调用Firebase Cloud函数

时间:2019-09-28 19:14:43

标签: firebase google-cloud-functions aws-cloud9

我知道这个问题已经问了很多遍了,我已经阅读了所有答案,但是仍然没有有效的解决方案。

我尝试运行自己的firebase函数并使用override func viewDidLoad() { super.viewDidLoad() self.loadJSONOne() self.loadJSONTwo() } func loadJSONOne() { // JSON Decoding with GET call here for URL one } func loadJSONTwo() { // JSON Decoding with GET call here for URL two } firebase serve as described herehere在本地托管。

这给了我类似的东西

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

从本地托管站点尝试调用已部署的云功能并给我一个CORS错误:

  

CORS政策禁止从来源“ https://us-central1-XXXXX.cloudfunctions.net/log”访问“ http://127.0.0.1:8080

我正在使用firebase auto configuration加载sdk。

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

1 个答案:

答案 0 :(得分:0)

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

也许这也是您的问题?