【发布时间】:2021-06-29 20:32:33
【问题描述】:
我正在使用来自 firebase 的函数和托管。
我已经定义了一个函数,如下所示。
const functions = require("firebase-functions")
const cors = require('cors')
exports.hello = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true})
return cors()(request, response, () => {
response.send({data: 'hello fire functions'})
})
})
在托管中调用这样的函数:
import firebase from "firebase/app"
import "firebase/functions"
const config = { ... }
firebase.initializeApp( config )
const test = firebase.functions().httpsCallable('hello')
test().then( result => console.log(result) )
那么函数日志会被写入两次,如下:
2:37:07.548 PM hello: Function execution started
2:37:07.599 PM hello: Hello logs!
2:37:07.600 PM hello: Function execution took 53 ms, finished with status code: 204
2:37:07.809 PM hello: Function execution started
2:37:07.816 PM hello: Hello logs!
2:37:07.817 PM hello: Function execution took 8 ms, finished with status code: 200
它也在使用图中显示两次。
这种行为意味着我必须支付两倍的使用量。这不正常。
如果cors没有被使用,日志和使用图会显示它只执行了一次。
但是如果你不使用cors:当你在浏览器中调用一个函数时,该函数被执行,但是浏览器得到一个CORS错误。
我该如何解决这个问题?我在官方文档中找不到解决方案。 (这是托管和功能部署后的问题。不是localhost环境。)
【问题讨论】:
标签: firebase google-cloud-functions