我正在尝试在本地测试我的云功能。我有一个自定义函数配置变量,因此我使用了命令firebase functions:config:get > .runtimeconfig.json
来生成runtimeconfig.json
文件:
{
"stripe": {
"testkey": "someKey"
}
}
然后我使用命令firebase functions:shell
启动仿真器,但是它将在我的终端上显示以下错误:
+ functions: Emulator started at http://localhost:5001
! TypeError: Cannot read property 'testkey' of undefined
at Object.<anonymous> (C:\Users\Jesper\intergun\functions\lib\index.js:14:52)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:532:33
at Generator.next (<anonymous>)
! We were unable to load your functions code. (see above)
- It appears your code is written in Typescript, which must be compiled before emulation.
- You may be able to run "npm run build" in your functions directory to resolve this.
由于某种原因,它找不到变量,并且似乎也认为代码是用TypeScript编写的,即使它指向生成的index.js
文件也是如此。我尝试运行npm run build
,但结果相同。
我的index.ts
的顶部看起来像这样:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import 'firebase-functions';
import * as Stripe from 'stripe';
admin.initializeApp();
const stripe = new Stripe(functions.config().stripe.testkey);
我想念什么?
答案 0 :(得分:0)
我在这里找到了解决方法:https://github.com/firebase/firebase-tools/issues/711
要生成.runtimeconfig.json
,我使用了命令firebase functions:config:get > .runtimeconfig.json
。但是,这生成了一个看似正确的文件,但显然具有不应该包含的不可见字符。相反,我使用了以下命令:firebase functions:config:get | ac .runtimeconfig.json
。这样就解决了问题。