【问题标题】:Can't deploy functions using google cloud functions-emulator无法使用谷歌云功能模拟器部署功能
【发布时间】:2019-10-28 07:30:01
【问题描述】:

我在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
}

我该如何解决这个问题?

【问题讨论】:

    标签: firebase google-cloud-platform google-cloud-functions gcloud


    【解决方案1】:

    您使用Firebase CLI 提供的emulators 来测试使用 Firebase SDK 编写的函数。云模拟器 (@google-cloud/functions-emulator) 将无法工作。

    很遗憾,您正在编写一个实时数据库函数,模拟器目前不支持该函数。

    【讨论】:

    • 那么我绝对没有办法调试我的函数吗?
    • 您可以使用它在本地直接调用您的函数。 firebase.google.com/docs/functions/local-shell
    • 谢谢。我尝试运行firebase functions:shell,现在它不再抱怨FIREBASE_CONFIGGCLOUD_PROJECT。但是,它仍然找不到我的环境变量stripe.testkey...收到此错误:TypeError: Cannot read property 'testkey' of undefined。请帮忙:-/
    • 另外,由于某种原因,它告诉我这个:It appears your code is written in Typescript, which must be compiled before emulation...
    • 您现在的问题与您最初提出的问题不同,因此请发布一个新问题。您的第一个问题已经解决(云模拟器将无法工作)。
    猜你喜欢
    • 2017-08-15
    • 2021-02-26
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2019-08-30
    • 2022-09-23
    • 2019-01-30
    • 2018-09-14
    相关资源
    最近更新 更多