在本地运行云功能会出现错误"functions.config()不可用"

tem*_*mp_ 35 node.js firebase google-cloud-functions

Firebase的Cloud Functions刚刚发布,我正在按照全新安装的说明进行操作.这是"开始使用"页面.

我已经安装了"npm install -g firebase-tools",我的所有文件都在我的项目中.我正在使用WebStorm 2016.3和Node.JS v6.10.0.

我安装了firebase登录和firebase init函数并进行了设置.我的设置. 这是我的设置.

我的package.json

 {
   "name": "functions",
   "description": "Cloud Functions for Firebase",
    "dependencies": {
     "firebase-admin": "^4.1.2",
     "firebase-functions": "^0.5"
   },
    "private": true
 }
Run Code Online (Sandbox Code Playgroud)

前两行代码工作正常.

  const functions = require('firebase-functions');
  const admin = require('firebase-admin');
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行这条线时......

  admin.initializeApp(functions.config().firebase);
Run Code Online (Sandbox Code Playgroud)

我收到这个错误.

 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)
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

小智 53

如果像我一样,在尝试在本地运行函数时遇到此错误,则因为functions.config()它仅在Cloud Functions运行时中可用.

如果您在部署之前尝试测试功能,请参阅文档的链接,了解如何执行此操作:在本地运行功能.具体来说,这部分是有意义的:

如果您正在使用自定义函数配置变量,请在运行firebase serve之前在项目的functions目录中运行以下命令.

firebase functions:config:get > .runtimeconfig.json
Run Code Online (Sandbox Code Playgroud)

但是,如果您使用的是Windows PowerShell,请将以上命令替换为:

firebase functions:config:get | ac .runtimeconfig.json
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,它正在使用其他环境变量.我在相邻页面上找到了答案,该页面表示创建一个本地运行时配置,该配置复制实际的配置文件,因为配置仅适用于部署的代码.对于local,运行这个`firebase functions:config:get> .runtimeconfig.json`. (12认同)
  • @MartavisP.你救了我的一天!重要说明:.runtimeconfig.json应该放在"functions"文件夹中,因为那个问题也因为最初放到root中. (10认同)

Rob*_*man 6

firebase即使您尝试安装最新版本,看起来您可能仍然拥有旧版本的CLI firebase-tools.

你可以通过跑步来检查firebase --version.至少应该这样说3.5.0.如果没有,你会想npm install -g firebase-tools再次运行,这应该有希望解决问题.

如果您firebase --version继续显示错误的版本,则需要检查是否意外安装了多个版本firebase-tools.


Dou*_*oug 5

firebase deploy从错误的目录运行。希望这可以节省一些时间。