javascript - Firebase 函数配置不会部署
问题描述
我正在尝试根据我在部署之前设置的环境变量部署一个使用 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
没有拾取我在部署到生产之前设置的 env 变量,而它在登台时完美地做到了这一点。
如果我设置app.env
为“暂存”,它对我的暂存环境非常有用,并且不会显示任何错误。我尝试cd
进入函数文件夹以设置环境变量,然后在部署之前退出,但它没有改变任何事情。我什至尝试将它写入.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"
}
}
解决方案
我首先切换到生产项目,设置变量,然后从那里部署来解决这个问题。
firebase use production
firebase functions:config:set someservice.key="keyvalue" ...
firebase deploy --only functions
即使我之前确实-P
在部署命令中使用了该标志(firebase -P production
- 请参阅问题),但显然没有设置适当的配置值。
推荐阅读
- python - 如何处理flask-socketio中的事件?
- javascript - 以漂亮的 HTML 格式显示 JSON
- java - 使用内置排序功能对二维数组进行排序
- c - 在makefile中使用ar(1)命令时将库提取到不同的路径
- python - 如何根据 python 中的多个标准连接两个数据框?
- sql-server - 如何在 1 个查询中使用 Select、Count 和 Sum 连接查询
- python - python tkinter按钮命令中使用或不使用lambda函数的区别
- javascript - 如何对数组进行j操作?
- python - 从python列表中的字符串末尾删除特定字符
- python - 使用 JPEG 压缩 (python) 保存多通道 (n > 3) 图像