为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复节点中的“无法读取未定义的属性‘修剪’”错误

如何修复节点中的“无法读取未定义的属性‘修剪’”错误

慕田峪9158850 2021-06-06 08:51:40
我正在使用 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/express/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/route.js:112:3)>      at Layer.handle [as handle_request] (/Users/kenny/Test/firebase_functions/functions/node_modules/express/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/router/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/init.js:40:5)我该如何解决这个错误?
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信