如何使用 expressjs 通过 body 参数传递通用变量来更新属性
how to update an attribute by passing generic variable through body parameter using expressjs
我正在为博客应用程序创建 restful API。
我必须使用邮递员更新博客的属性。必须更新属性的数据库中的 object 看起来像这样:
{
"_id": "5a5306a9432f7b12f6e77a21",
"title": "new article",
"_author": {
"username": "linuxuser"
},
"content": "content of the article goes here",
"__v": 0,
"createdAt": "2018-01-08T05:48:41.683Z"
}
我可能必须更新任何属性,但我无法在代码中动态处理它。
代码如下:
put('/updateblog', function(req, res) {
db.blogModel.findById(req.query.id, function(err, blog) {
if (err) {
res.send(err);
}
blog.title = req.body.title;
// here instead to title i have to update the attribute which is passed through body parameter
blog.save(function(err) {
if (err) {
res.send(err);
}
res.json({
message: "blog updated"
});
});
});
});`
这里我不想更新标题,而是想更新通过 body 参数传递的属性。我不知道客户端要发送什么,所以,我正在寻找更通用的代码。
例如,用户发送了更新 content
属性的放置请求,那么我该如何编写代码以便更新相应的属性而不是上面代码中的标题。
您可以从 req.body
获取键并迭代,或者如果 req.body
将仅包含您集合中 现有字段 的用户更新键和值,您可以直接将其用作更新对象,但更喜欢使用 req.body.userUpdate
等路径进行用户更新。
var updateObject = {};
for ( var key in req.body ) { //add logic to filter non existing fields
updateObject[key] = req.body[key] //change path if required
}
创建更新对象后使用findByIdAndUpdate
更新
findByIdAndUpdate(req.query.id, {$set : updateObject } , function(err, blog ) {...}
在 mongo CLI 中
> db.x.find().pretty()
{
"_id" : "5a5306a9432f7b12f6e77a21",
"title" : "new article",
"_author" : {
"username" : "linuxuser"
},
"content" : "content of the article goes here",
"__v" : 0,
"createdAt" : "2018-01-08T05:48:41.683Z"
}
更新对象
> var mod = {}
> mod['content']='new content'
new content
更新
> db.x.update({"_id": "5a5306a9432f7b12f6e77a21"}, {$set : mod}, {$upsert:false})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
输出
> db.x.find().pretty()
{
"_id" : "5a5306a9432f7b12f6e77a21",
"title" : "new article",
"_author" : {
"username" : "linuxuser"
},
"content" : "new content",
"__v" : 0,
"createdAt" : "2018-01-08T05:48:41.683Z"
}
>
我正在为博客应用程序创建 restful API。 我必须使用邮递员更新博客的属性。必须更新属性的数据库中的 object 看起来像这样:
{
"_id": "5a5306a9432f7b12f6e77a21",
"title": "new article",
"_author": {
"username": "linuxuser"
},
"content": "content of the article goes here",
"__v": 0,
"createdAt": "2018-01-08T05:48:41.683Z"
}
我可能必须更新任何属性,但我无法在代码中动态处理它。
代码如下:
put('/updateblog', function(req, res) {
db.blogModel.findById(req.query.id, function(err, blog) {
if (err) {
res.send(err);
}
blog.title = req.body.title;
// here instead to title i have to update the attribute which is passed through body parameter
blog.save(function(err) {
if (err) {
res.send(err);
}
res.json({
message: "blog updated"
});
});
});
});`
这里我不想更新标题,而是想更新通过 body 参数传递的属性。我不知道客户端要发送什么,所以,我正在寻找更通用的代码。
例如,用户发送了更新 content
属性的放置请求,那么我该如何编写代码以便更新相应的属性而不是上面代码中的标题。
您可以从 req.body
获取键并迭代,或者如果 req.body
将仅包含您集合中 现有字段 的用户更新键和值,您可以直接将其用作更新对象,但更喜欢使用 req.body.userUpdate
等路径进行用户更新。
var updateObject = {};
for ( var key in req.body ) { //add logic to filter non existing fields
updateObject[key] = req.body[key] //change path if required
}
创建更新对象后使用findByIdAndUpdate
更新
findByIdAndUpdate(req.query.id, {$set : updateObject } , function(err, blog ) {...}
在 mongo CLI 中
> db.x.find().pretty()
{
"_id" : "5a5306a9432f7b12f6e77a21",
"title" : "new article",
"_author" : {
"username" : "linuxuser"
},
"content" : "content of the article goes here",
"__v" : 0,
"createdAt" : "2018-01-08T05:48:41.683Z"
}
更新对象
> var mod = {}
> mod['content']='new content'
new content
更新
> db.x.update({"_id": "5a5306a9432f7b12f6e77a21"}, {$set : mod}, {$upsert:false})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
输出
> db.x.find().pretty()
{
"_id" : "5a5306a9432f7b12f6e77a21",
"title" : "new article",
"_author" : {
"username" : "linuxuser"
},
"content" : "new content",
"__v" : 0,
"createdAt" : "2018-01-08T05:48:41.683Z"
}
>