簡體   English   中英

Firebase 功能配置不會部署

[英]Firebase functions config won't deploy

我正在嘗試部署一個 function,它使用基於部署前設置的環境變量的 firebase 配置變量。 這在暫存時效果很好(一個看起來幾乎與生產相同的單獨項目,除了幾個 env var),但在生產中失敗並出現錯誤TypeError: Cannot read property 'env' of undefined指向functions.config().app.env (見下面的代碼)。

If I browse the function online in https://console.cloud.google.com/functions/details/us-central1/<functionName>?project=<projectName> the .runtimeconfig.json file appears to not have picked up the env我在部署到生產之前設置的變量,而它在登台時完美地做到了這一點。

如果我將app.env設置為“staging”,它適用於我的 staging 環境,並且不會顯示任何錯誤。 我嘗試cd -ing 進入函數文件夾以設置 env 變量,然后在部署之前退出,但它沒有改變任何事情。 我什至嘗試將它寫入函數文件夾中的.runtimeconfig.json (盡管這應該只需要本地測試),但這也沒有效果。

編輯:經過一些更多的測試后,我發現在在線瀏覽 function 代碼時可以看到的.runtimeconfig是從我通過 CLI 本地設置到函數配置的內容生成的,並在我部署時上傳。 每當我更新它時,它似乎都可以很好地復制到登台上,但在生產中卻沒有這樣做。 我就是想不通為什么...

我的 function:

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 的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 的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 - 見問題),但顯然沒有設置適當的配置值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM