无法使用Google Cloud Functions-emulator部署功能

时间:2019-06-13 16:33:29

标签: firebase google-cloud-functions gcloud

我在index.ts文件中定义了以下函数:

export const stripeCharge = functions.region('europe-west1').database
.ref('/payments/{userId}/{paymentId}')
.onWrite(async (change, context) => {
    ...
});

我想调试此功能,所以我尝试使用Google Cloud Functions Emulator(npm install -g @ google-cloud / functions-emulator)。

首先我跑步:

functions start

启动仿真器。

然后我要部署该功能:

functions deploy --trigger-http --timeout 600s stripeCharge

这会导致以下错误:

ERROR: Function load error: Code could not be loaded.
ERROR: Does the file exists? Is there a syntax error in your code?
ERROR: Detailed stack trace: Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail
C:\Users\Jesper\intergun\functions\lib\index.js:14
const stripe = new Stripe(functions.config().stripe.testkey);
                                                   ^

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 [eval]:1:40
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)

ERROR: Error: Failed to deploy function.
    at exec (C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\@google-cloud\functions-emulator\src\cli\controller.js:126:22)
    at ChildProcess.exithandler (child_process.js:288:5)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:915:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)

所以我认为问题是它找不到FIREBASE_CONFIG和GCLOUD_PROJECT,我不明白,因为它们应该根据以下内容自动填充:https://firebase.google.com/docs/functions/config-env

这是我的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);

我还有一个.runtimeconfig.json文件,其中包含以下内容:

{
  "stripe": {
    "testkey": "..."
  }
}

最后,这是我的package.json

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/storage": "^2.5.0",
    "firebase-admin": "^8.1.0",
    "firebase-functions": "^3.0.1",
    "stripe": "^7.1.0"
  },
  "devDependencies": {
    "@types/sharp": "^0.22.2",
    "@types/stripe": "^6.30.0",
    "firebase-functions-test": "^0.1.6",
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },
  "private": true
}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您可以使用emulators提供的Firebase CLI来测试使用Firebase SDK编写的功能。云模拟器(@ google-cloud / functions-emulator)无法正常工作。

不幸的是,您正在编写Realtime Database函数,该仿真器当前不支持该功能。