admin管理员组文章数量:1316509
I am sending an array of objects to the server (as below), the server must receive the objects and add them to the mongodb. I'm new to node.js, I usually deal with the keys inside the rcevived request body (req.body). But here, the keys are the objects. How can I iterate over them?
[
{
id: "1",
Name: "John"
},
{
id: "2",
Name: "Mark"
},
{
id: "3",
Name: "Domi"
}
]
Server Code:
server.get('/user', function (req, res, next) {
//iterate over the objects in req.body
});
When I want to send one object I can easily get the the request content by req.body.id and req.body.Name, so how to do it with multiple objects inside the request body?
I am sending an array of objects to the server (as below), the server must receive the objects and add them to the mongodb. I'm new to node.js, I usually deal with the keys inside the rcevived request body (req.body). But here, the keys are the objects. How can I iterate over them?
[
{
id: "1",
Name: "John"
},
{
id: "2",
Name: "Mark"
},
{
id: "3",
Name: "Domi"
}
]
Server Code:
server.get('/user', function (req, res, next) {
//iterate over the objects in req.body
});
When I want to send one object I can easily get the the request content by req.body.id and req.body.Name, so how to do it with multiple objects inside the request body?
Share Improve this question edited Feb 25, 2015 at 20:52 zoma.saf asked Feb 25, 2015 at 16:56 zoma.safzoma.saf 5683 gold badges6 silver badges14 bronze badges 4-
2
Are you sure those outer
{}
s aren't a[]
? It's probably anArray
. – Jacob Commented Feb 25, 2015 at 17:00 - Possible duplicate - stackoverflow./questions/7440001/… – user3970381 Commented Feb 25, 2015 at 17:01
-
You have mentioned in title as
Array of objects
..but your code doesn't depicts the same.. – Rakesh_Kumar Commented Feb 25, 2015 at 17:03 - @Jacob fixed for convenient understanding – zoma.saf Commented Feb 25, 2015 at 20:52
1 Answer
Reset to default 4something like this:
var bodyParser = require('body-parser');
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
server.get('/user', function (req, res, next) {
var data = req.body;
data.forEach(function (item) {
console.log(item.id);
console.log(item.Name);
});
});
本文标签: javascriptHow to iterate over an array of objects in the request bodyStack Overflow
版权声明:本文标题:javascript - How to iterate over an array of objects in the request body - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741980881a2408388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论