检查空对象文字

时间:2018-10-25 12:25:32

标签: javascript node.js mocha

如果req.body = {}如何检查文字是否为空并显示错误消息?

代码:

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

以上是我要实现的目标,但是由于某种原因,即使您在console.log = {}上使用req.body,它也通过了验证。

如何显示该状态?

0 个答案:

没有答案