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

ken*_*ken 6 javascript node.js firebase typescript google-cloud-functions

我正在使用 TypeScript 在 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
}

Run Code Online (Sandbox Code Playgroud)

构建将打字稿转换为 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)

Run Code Online (Sandbox Code Playgroud)

我该如何修复这个错误?

ajo*_*era 1

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

快速修复:设置默认值

const isEmpty = (str=''): boolean => {
    // code...
}
Run Code Online (Sandbox Code Playgroud)

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

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

您正在使用 API 端点来创建新用户,并且您将需要一些数据以及您要创建的用户的名字、姓氏、年龄和出生日期等请求。显然,传递 Sally 作为年龄值或 53 作为出生日期不会让事情朝着正确的方向发展。您确实不希望不良数据通过您的应用程序,那么您该怎么办?答案是数据验证。 参考

我将使用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...
Run Code Online (Sandbox Code Playgroud)