【发布时间】:2021-04-21 23:03:32
【问题描述】:
我正在尝试根据我在部署之前设置的环境变量部署一个使用 firebase 配置变量的函数。这在暂存时效果很好(一个看起来几乎与生产相同的单独项目,除了几个环境变量),但在生产中失败,错误 TypeError: Cannot read property 'env' of undefined 指向 functions.config().app.env(参见下面的代码)。
如果我在 https://console.cloud.google.com/functions/details/us-central1/<functionName>?project=<projectName> 中在线浏览函数,.runtimeconfig.json 文件似乎没有获取我在部署到生产之前设置的环境变量,而它在暂存时完美地做到了。
如果我将app.env 设置为“暂存”,它对我的暂存环境非常有效,并且不会显示任何错误。我尝试cd-ing 进入函数文件夹以设置环境变量,然后在部署之前退出,但它没有改变任何事情。我什至尝试将其写入函数文件夹中的.runtimeconfig.json(尽管这应该只用于本地测试),但这也没有效果。
编辑:经过更多测试,发现.runtimeconfig在线浏览功能代码时可以看到是从我通过CLI本地设置到功能配置的内容生成的,并在上传时我部署。看来,每当我更新它时,它都可以很好地复制到登台上,但在生产中却没有这样做。我就是不知道为什么……
我的功能:
const functions = require('firebase-functions')
// Stripe needs to be set with its key when the library is being required
const stripe = require("stripe")(functions.config().app.env === 'staging' ? functions.config().stripe.key.staging : functions.config().stripe.key.production)
// ^^^ this is what the error is pointing to
module.exports = functions.https.onCall(async (data, context) => {
// ... doin' some stripe magic
})
我在 shell 命令行环境中使用 npm:
firebase functions:config:set app.env="production" && firebase deploy -P production --only functions
firebase use 的输出:
% firebase use
Active Project: default (<stagingProjectName>)
Project aliases for <projectFolderPath>:
* default (<stagingProjectName>)
production (<productionProjectName>)
Run firebase use --add to define a new project alias.
firebase functions:config:get 的输出(以及其他变量):
{
"app": {
"env": "production"
}
}
【问题讨论】:
标签: javascript firebase google-cloud-functions