0

If req.body = {} how do i do a check to see the literal is empty and show error messages ?

Code:

// UPDATES A SINGLE BRAND IN THE DATABASE
router.patch('/:id', (req, res) => {
var id = req.params.id;

//only params user can update, if not listed it cant update
var body = _.pick(req.body, ['name', 'about', 'pickup_address', 'pickup_address2', 'pickup_address_city', 'pickup_address_state', 'pickup_address_country', 'pickup_address_code', 'pickup_telephone']);

if(!ObjectID.isValid(id)){
    return res.status(404).send({message: 'Invalid Object ID provided'})
}

console.log(Object.keys(req.body).length);

if (!("name" in req.body)) {
    return res.status(204).send({message: 'No Content'});       
}

//console.log(res.params.body);
console.log(req.body);

//The $set operator replaces the value of a field with the specified value.
Brand.findByIdAndUpdate(id, {$set: body}, {new: true}).then((brand) => {
    if(!brand){
        return res.status(404).send({message: 'Error, brand not found'});
    }
    res.send({brand});
}).catch((e) => {
    return res.status(404).send(e);
});
});

The above is what im trying to achieve but for some reason it passes that validation even thou req.body on console.log = {}.

How to get that status to show?

Ylama
  • 2,449
  • 2
  • 25
  • 50
  • 2
    `{}` is an *object literal*, not an array. Objects do not have a native "length" property like arrays do. – Pointy Oct 25 '18 at 12:27
  • `Object.keys(req.body).length === 0` – Ihor Sakailiuk Oct 25 '18 at 12:28
  • You should set the req.body as `[]`. Ex: `req.body = []` – Martin Solev Oct 25 '18 at 12:28
  • @MartinSolev `req.body` is probably something passed in to the code in context of the question, and just setting it to an empty array would preclude any need for testing to see if it's empty. – Pointy Oct 25 '18 at 12:29
  • "how do i do a check to see the aray is empty"....you can't because it's not an array, it's an object. You need to learn the difference between the syntaxes to avoid similar confusion in future. – ADyson Oct 25 '18 at 12:32
  • @Pointy thanks for pointing that out ill change the question – Ylama Oct 25 '18 at 12:42
  • @Ylama the key here is that "empty" is a pretty obvious concept for an array, but for objects in general it depends more on what your application expects from a particular kind of object in some situation. – Pointy Oct 25 '18 at 12:45
  • Thanks @Pointy learned something new again, so lets say its expecting `{ name: 'nameHere' }` but receives it as `{}` hw would i do a check on this object literal that has no data? – Ylama Oct 25 '18 at 12:49
  • Well if an object with no "name" property (or some collection of properties) makes no sense for your application, you can just check `if (!("name" in obj))` to decide that it is, for your purposes, "empty". – Pointy Oct 25 '18 at 12:51
  • @Pointy im updateing my question , im doing a patch request, thing is whenever the user wants to patch a brand but provides no content i want to show error status 204 – Ylama Oct 25 '18 at 12:57
  • Well what exactly doesn't work? If the `"name" in req.body` test passes, then the object must have a "name" property. Now, I suppose it could be `null` or the empty string, so you could check that too. – Pointy Oct 25 '18 at 13:02
  • if i pass name as JSON `{ name: 'nameHere' }` the `req.body = { name: 'nameHere' }` but if i pass nothing no key no value, then the `req.body = {}` , htats when i want to show error status 204 – Ylama Oct 25 '18 at 13:13
  • 1
    Right, so the updated code looks like it includes the test you want. What doesn't work? – Pointy Oct 25 '18 at 13:26
  • bro @Pointy thanks for the help, i realized status 204 doesnt return a body.... – Ylama Oct 25 '18 at 14:08
  • 1
    `if (Object.keys(req.body).length === 0) { return res.status(204).send(); }` this is what worked – Ylama Oct 25 '18 at 14:09
  • Great! I'm glad you're up and running. – Pointy Oct 25 '18 at 14:12

0 Answers0