首页 > 解决方案 > 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"
  }
}

标签: javascriptfirebasegoogle-cloud-functions

解决方案


我首先切换到生产项目,设置变量,然后从那里部署来解决这个问题。

firebase use production
firebase functions:config:set someservice.key="keyvalue" ...
firebase deploy --only functions

即使我之前确实-P在部署命令中使用了该标志(firebase -P production- 请参阅问题),但显然没有设置适当的配置值。


推荐阅读