最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fix "Cannot read property 'trim' of undefined" error in node - Stack Overf

programmeradmin0浏览0评论

I'm implementing a simple WebAPI in Firebase cloud functions with express by TypeScript. My code is following.

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
}

Build to transform typescript to javascript worked fine. However, when I did POST method, following error occurred.

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

How can I fix this error ?

I'm implementing a simple WebAPI in Firebase cloud functions with express by TypeScript. My code is following.

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
}

Build to transform typescript to javascript worked fine. However, when I did POST method, following error occurred.

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

How can I fix this error ?

Share Improve this question edited May 27, 2019 at 6:25 hashed_name 5517 silver badges22 bronze badges asked May 27, 2019 at 4:51 kenken 6682 gold badges7 silver badges17 bronze badges 2
  • Sounds like the text is empty. Is the content type text? – Vaughan Hilts Commented May 27, 2019 at 4:52
  • 1 The variable 'text' holds nothing.Because 'req.body.text' has undefined value. Check with your 'req.body' whether having value or not. – Thamarai T Commented May 27, 2019 at 5:06
Add a ment  | 

4 Answers 4

Reset to default 1

Your problem is that isEmpty is not prepared to receive things that are not a string.

Quick fix: set a default value

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

Better solution: Validate your data.

While developing any API, you need to validate the entry data to your request.

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.

I'll make a quick example for this case using 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...

First thing you need to see why req.body.text is null or undefined, then you do the empty check like below.

const isEmpty = (str: string): boolean => {
    console.log("str: ", str, "+++")
    const result = (!str || str.toString().trim() === ""); // converting toString because there chance body.text can be a number
    console.log("result: ", result)
    return result
}

Just check is str is actually empty in isEmpty Add if(!str) return true to it. It will look like this:

const isEmpty = (str: string): boolean => {
    console.log("str: ", str, "+++")
    if(!str) return true;
    const trimedStr = str.trim()
    const result = (trimedStr === null || trimedStr === "")
    console.log("result: ", result)
    return result
}

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).

so why can't you use like below ?

 if (!text) {
        resText = "Empty"
    }
发布评论

评论列表(0)

  1. 暂无评论