定义简单函数时出现谷歌云函数错误

时间:2019-02-04 10:57:01

标签: firebase google-cloud-functions firebase-hosting

嗨,我正在尝试使用Firebase托管部署一个简单的函数,当我这样做时,我的Firebase日志中会出现以下错误:

TypeError: Cannot set property 'content-type' of undefined
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:365:22)
    at ServerResponse.header (/user_code/node_modules/express/lib/response.js:767:10)
    at ServerResponse.contentType (/user_code/node_modules/express/lib/response.js:595:15)
    at ServerResponse.send (/user_code/node_modules/express/lib/response.js:145:14)
    at app.get (/user_code/lib/index.js:10:22)
    at Layer.handle [as handle_request] (/user_code/node_modules/express/lib/router/layer.js:95:5)
    at next (/user_code/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/user_code/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/user_code/node_modules/express/lib/router/layer.js:95:5)
    at /user_code/node_modules/express/lib/router/index.js:281:22

由于我刚开始使用Firebase托管,所以我不知道是什么原因造成的。这是我的文件:

src> index.ts:

import * as functions from 'firebase-functions';
import * as express from 'express';

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//

const app = express();

export const getTime = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

firebase.json:

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites":[{
      "source": "/timestamp",
      "function": "getTime"
    }]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ]
  }
}

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"
  },
  "main": "lib/index.js",
  "dependencies": {
    "express": "^4.16.4",
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.1.0"
  },
  "devDependencies": {
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },
  "private": true
}

1 个答案:

答案 0 :(得分:0)

您可能会看到here,您的index.ts应该是:

import * as functions from 'firebase-functions';
import * as express from 'express';

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//

const app = express();

app.get("*", (request, response) => {
    response.send("Hello from Firebase!");
});

export const getTime = functions.https.onRequest(app);