gpt4 book ai didi

javascript - 如何修复 Node 中未定义错误的 "Cannot read property ' trim'

转载 作者:搜寻专家 更新时间:2023-10-30 21:00:44 24 4
gpt4 key购买 nike

我正在 Firebase 云函数中通过 TypeScript 实现一个简单的 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
}

将 typescript 转换为 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)

如何修复此错误?

最佳答案

你的问题是 isEmpty 没有准备好接收非字符串的东西。

快速修复:设置默认值

const isEmpty = (str=''): boolean => {
// code...
}

更好的解决方案:验证您的数据。

在开发任何 API 时,您需要验证您的请求的输入数据。

You are working on an API endpoint to create a new user and you will require some data along with the request like firstname, lastname, age and birthdate for the user you are about to create. Obviously, passing Sally as value for age or 53 for birthdate won't get things rolling in the right direction. You really don't want bad data making its way through your application so what do you do? The answer is Data Validation. Reference.

我将使用 Joi 为这种情况做一个简单的例子:

// code...

const schema = {
text: Joi.string()
};

app.post('/', (req, res) => {
//my code starts here
const data = req.body;
const {error, value} = Joi.validate(data, schema);

if(error) {
return res.status(400).json(error);
}
// ends here


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)
});

// code...

关于javascript - 如何修复 Node 中未定义错误的 "Cannot read property ' trim',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com