2017-03-16 27 views
21

的火力地堡雲功能是最近剛剛發佈,我下面就一個新的說明進行安裝。Here is the "Get Started" page.「functions.config()不可用‘與火力地堡如何使用Node.js雲功能

我已經安裝了’ npm install -g firebase-tools「和我所有的文件都在我的項目中,我正在使用WebStorm 2016.3和Node.JS v6.10.0。

我已經安裝並設置了firebase登錄和firebase init函數。 我的設置 Here is my set up.

我的包age.json

{ 
    "name": "functions", 
    "description": "Cloud Functions for Firebase", 
    "dependencies": { 
    "firebase-admin": "^4.1.2", 
    "firebase-functions": "^0.5" 
    }, 
    "private": true 
} 

這些前兩行代碼工作。

const functions = require('firebase-functions'); 
    const admin = require('firebase-admin'); 

但是當我嘗試運行這條線......

admin.initializeApp(functions.config().firebase); 

我得到這個錯誤。

Error: functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function. 
    at init (/Users/.../functions/node_modules/firebase-functions/lib/config.js:46:19) 
    at Object.config (/Users/.../functions/node_modules/firebase-functions/lib/config.js:29:9) 
    at Object.<anonymous> (/Users/.../functions/index.js:11:31) 
    at Module._compile (module.js:570:32) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.runMain (module.js:604:10) 
    at run (bootstrap_node.js:394:7) 

我在這裏錯過了什麼?

回答

24

如果你和我一樣,並得到這個錯誤試圖在本地運行你的函數,那麼你會得到這個錯誤,因爲functions.config()只在雲功能運行時間內可用。如果您在部署之前試圖測試您的功能,請參閱以下文檔的鏈接:https://firebase.google.com/docs/functions/unit-testing#mocking_database_writes

+3

對我而言,它使用了其他環境變量。我在相鄰頁面上找到了答案,該頁面說創建本地運行時配置以複製實際的配置文件,因爲該配置僅適用於部署的代碼。對於本地,運行這個'firebase functions:config:get> .runtimeconfig.json'。 –

+0

這幫助我瞭解如何在部署之前在本地進行測試:https://firebase.google.com/docs/functions/local-emulator – cking24343

+0

@MartavisP。你救了我的一天!重要說明:.runtimeconfig.json應放置在「functions」文件夾內,因此也有問題,因爲最初放在根目錄下。 –

6

即使您嘗試安裝最新的firebase-tools,看起來好像您仍可能擁有舊版本的firebase CLI。

您可以通過運行firebase --version進行檢查。那應該說至少3.5.0。如果沒有,你會想再次運行npm install -g firebase-tools,這應該有望解決問題。

如果您的firebase --version繼續顯示錯誤的版本,您需要檢查是否意外安裝了多個版本的firebase-tools

+0

是的,我有3.5.0。我會環顧四周,看看我是否可以卸載Firebase並重新安裝。另外,我的firebase.json文件是空白的,對嗎? –

+0

我不希望該文件是空白的。即使'firebase.json'中沒有值,它也會包含'{}'。你可能想嘗試再次運行'firebase init'。 –

+0

對我來說,它沒有奏效。我需要手動編寫config.json文件。請查看https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/74 – ByteArtisan

1

我從錯誤的目錄運行firebase部署。希望這可以節省一些時間。