在NodeJS ExpressJS中从req.body中删除一个variables

我收到很多信息的JSON,但我想要删除特定的数据,例如,如果我收到variables名称,我想删除此variables,然后添加我的数据库中的JSON。 这是我的functionPOST:

app.post("/:coleccion", function (req, res, next) { POST if(req.body.name) // Here I need delete "name" if I receive this variable <------ req.collection.insert(req.body, {}, function (e, result) { if(e) return next(e); res.send(result); }); }); 

 delete req.body.name 

应该在一个javascript对象中工作

检查如何从JavaScript对象中删除属性?

我会敦促你只挑选你想要的属性,而不是去除那些你不需要的属性。 否则,客户端可以发送任何想要的东西,包括以$开头的字段,这些字段将被MongoDB特别处理。

这是最简单的方法:

 var data = { email: req.body.email, age: req.body.age, gender: req.body.gender } req.collection.insert(data, ...) 

你也可以使用Underscore.js来完成这个工作:

 var data = _.pick(req.body, 'email', 'age', 'gender') req.collection.insert(data, ...)