Man*_*anu 1 javascript json request node.js express
我收到一个包含大量信息的JSON,但我想删除一个特定的数据,例如,如果我收到变量名,我想在我的数据库中添加我的JSON之前删除这个变量.这是我的功能POST:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
我会敦促您只选择您真正想要的属性,而不是删除您不需要的属性。否则,客户端可以发送它想要的任何内容,包括$以 MongoDB开始特殊处理的字段。
这是最简单的方法:
var data = {
email: req.body.email,
age: req.body.age,
gender: req.body.gender
}
req.collection.insert(data, ...)
Run Code Online (Sandbox Code Playgroud)
你也可以使用Underscore.js来完成这项工作:
var data = _.pick(req.body, 'email', 'age', 'gender')
req.collection.insert(data, ...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4414 次 |
| 最近记录: |