javascript - How to check req.body empty or not in node ,express? - Stack Overflow

Below is my request body and which is sending from client side varcredentials = {"ConsumerData&q

Below is my request body and which is sending from client side

var  credentials = {

"ConsumerData": {

            "ConsumerStoreId": "a",
            "ConsumerUserId": "a"
        },
        "CustomerData": {

            "CustomerId": "2345678890"
        },
        "ErnD": {
            "UID": "3",
            "TxnDt": "1"
        },

        "PurD": [{
                "ItemCode": "3456tghw3",
                "ItemEANCode": "223222122"

            },
            {
                "ItemCode": "8jghw3865",
                "ItemEANCode": "3334443222"

            }
        ]
}

for testing i am sending var credentials = {} empty credentials

In server side controller(node,express) i want to check req.body empty or not

if(!req.body)

{
 console.log('Object missing');
}
if(!req.body.ConsumerData.ConsumerStoreId)
  {
  console.log('ConsumerStoreId missing');
  }
 if(!req.body.CustomerData.CustomerId)
  {
  console.log('CustomerId missing');
  }
  if(!req.body.ErnD.UID)
  {
  console.log('UID missing');
  }
    console.log('outside'); 

i am checking everything but alwasys its printing outside only

Below is my request body and which is sending from client side

var  credentials = {

"ConsumerData": {

            "ConsumerStoreId": "a",
            "ConsumerUserId": "a"
        },
        "CustomerData": {

            "CustomerId": "2345678890"
        },
        "ErnD": {
            "UID": "3",
            "TxnDt": "1"
        },

        "PurD": [{
                "ItemCode": "3456tghw3",
                "ItemEANCode": "223222122"

            },
            {
                "ItemCode": "8jghw3865",
                "ItemEANCode": "3334443222"

            }
        ]
}

for testing i am sending var credentials = {} empty credentials

In server side controller(node,express) i want to check req.body empty or not

if(!req.body)

{
 console.log('Object missing');
}
if(!req.body.ConsumerData.ConsumerStoreId)
  {
  console.log('ConsumerStoreId missing');
  }
 if(!req.body.CustomerData.CustomerId)
  {
  console.log('CustomerId missing');
  }
  if(!req.body.ErnD.UID)
  {
  console.log('UID missing');
  }
    console.log('outside'); 

i am checking everything but alwasys its printing outside only

Share Improve this question asked Mar 21, 2017 at 8:25 its meits me 5426 gold badges30 silver badges67 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 21

ES2015:

if(req.body.constructor === Object && Object.keys(req.body).length === 0) {
  console.log('Object missing');
}

PRE ES2015:

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }
    return JSON.stringify(obj) === JSON.stringify({});
}

if(isEmpty(req.body)) {
    console.log('Object missing');
}

For more ways in a pre es2015 style: https://coderwall.com/p/_g3x9q/how-to-check-if-javascript-object-is-empty

if (Object.keys(req.body).length === 0) {
   // Do something
}

When the req.body is empty, it returns an empty object, as such, making !req.body return false even when it's empty. Instead, you should test for !Object.keys(req.body).length. What it will do is take every key from the object and count, if no keys where found it would return 0. Then all we need to do is use the same method that we use on empty arrays, testing for !arr.length making it possible to receive a boolean stating it's fullness or the lack of it.

Then, we end up with the code:

router.post('/auth/signup', function(req, res) {
    if(!Object.keys(req.body).length) {
        // is empty
    } else if(!req.body.param1 || !req.body.param2 || !req.body.param3) {
        let params = [req.body.param1, req.body.param2, req.body.param3];
        let lackingParam = params.findIndex(param => !param === true) > 0 ? params.findIndex(param => !param === true) > 1 ? "req.body.param3" : "req.body.param2" : "req.body.param1";
        // lacking only lackingParam
    } else {
        // not empty
    }
});

What about using lodash.isempty?

const isEmpty = require('lodash.isempty');

if(isEmpty(req.body)) {
    console.log('Empty Object');
}

Here are the docs https://lodash.com/docs/4.17.11#isEmpty

Checks if value is an empty object, collection, map, or set.

Objects are considered empty if they have no own enumerable string keyed properties.

Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections are considered empty if they have a length of 0. Similarly, maps and sets are considered empty if they have a size of 0.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1737586260a3957971.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信