我正在使用 TypeScript 的 express 在 Firebase 云函数中实现一个简单的 WebAPI。我的代码如下。
import * as functions from 'firebase-functions';
import * as express from 'express';
const app = express()
app.post('/', (req, res) => {
var resText: string = "NotEmpty"
const text: string = req.body.text
if (isEmpty(text)) {
resText = "Empty"
}
console.log("text: ", text)
console.log("resText: ", resText)
res.send("Echo " + resText)
})
exports.api = functions.https.onRequest(app)
const isEmpty = (str: string): boolean => {
console.log("str: ", str, "+++")
const trimedStr = str.trim()
const result = (trimedStr === null || trimedStr === "")
console.log("result: ", result)
return result
}
构建以将打字稿转换为 javascript 工作正常。但是,当我执行 POST 方法时,发生了以下错误。
> TypeError: Cannot read property 'trim' of undefined
> at isEmpty (/Users/kenny/Test/firebase_functions/functions/lib/index.js:22:27)
> at app.post (/Users/kenny/Test/firebase_functions/functions/lib/index.js:9:9)
> at Layer.handle [as handle_request] (/Users/kenny/Test/firebase_functions/functions/node_modules/expr
ess/lib/router/layer.js:95:5)
> at next (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/route.js:137:
13)
> at Route.dispatch (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/rou
te.js:112:3)
> at Layer.handle [as handle_request] (/Users/kenny/Test/firebase_functions/functions/node_modules/expr
ess/lib/router/layer.js:95:5)
> at /Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/index.js:281:22
> at Function.process_params (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/r
outer/index.js:335:12)
> at next (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/router/index.js:275:
10)
> at expressInit (/Users/kenny/Test/firebase_functions/functions/node_modules/express/lib/middleware/in
it.js:40:5)
我该如何解决这个错误?
相关分类