node.js req.body 不能作为数组读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28764822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
req.body can't be read as an array
提问by zoma.saf
I'm using node.js to receive a post request, the request body has this content after printing it using console.log():
我正在使用 node.js 接收发布请求,请求正文在使用以下内容打印后具有以下内容console.log():
{
'object 1':
{
deviceType: 'iPad Retina',
guid: 'DF1121F9-FE66-4772-BE74-42936F1357FF',
is_deleted: '0',
last_modified: '1970-12-19T06:01:17.171',
name: 'test1',
projectDescription: '',
sync_status: '1',
userName: 'testUser'
},
'object 0':
{
deviceType: 'iPad Retina',
guid: '18460A72-2190-4375-9F4F-5324B2FCCE0F',
is_deleted: '0',
last_modified: '1970-12-19T06:01:17.171',
name: 'test2',
projectDescription: '',
sync_status: '1',
userName: 'testUser'
}
}
I'm getting the request using the below node.js code:
我正在使用以下 node.js 代码获取请求:
var restify = require('restify'),
mongoose = require('mongoose');
var connect = require('connect');
var bodyParser = require('body-parser');
/*server declaration
...
...
*/
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
server.post('/project', function (req, res, next) {
console.log(req.body);//the output is shown above
console.log(req.body.length);// --> output is undefined
//2
body.req.forEach(function (item) {//got an exception
console.log(item);
});
});
The second part of the code which has forEachfunction gives this exception "[TypeError: Object #<Object> has no method 'forEach']"
具有forEach函数的代码的第二部分给出了这个异常“ [TypeError: Object #<Object> has no method 'forEach']”
Do you know what am I missing?
你知道我错过了什么吗?
回答by Peter Herdenborg
req.bodyisn't an array, but an object with two properties. This is evident from the console.log output you've provided. Therefore, it has no lengthproperty and no forEachmethod.
req.body不是数组,而是具有两个属性的对象。从您提供的 console.log 输出中可以明显看出这一点。因此,它没有length属性,也没有forEach方法。
If it had been an array, it would have looked like this:
如果它是一个数组,它看起来像这样:
[
{
deviceType: 'iPad Retina',
guid: 'DF1121F9-FE66-4772-BE74-42936F1357FF',
is_deleted: '0',
last_modified: '1970-12-19T06:01:17.171',
name: 'test1',
projectDescription: '',
sync_status: '1',
userName: 'testUser'
},
{
deviceType: 'iPad Retina',
guid: '18460A72-2190-4375-9F4F-5324B2FCCE0F',
is_deleted: '0',
last_modified: '1970-12-19T06:01:17.171',
name: 'test2',
projectDescription: '',
sync_status: '1',
userName: 'testUser'
}
]
To iterate over the keys of the object you have, you can use the construct
要迭代您拥有的对象的键,您可以使用构造
for(var key in req.body) {
if(req.body.hasOwnProperty(key)){
//do something with e.g. req.body[key]
}
}
回答by Anurag Peshne
forEachis defined only for Arrays.
forEach仅针对数组定义。
You need to use for...inloop instead:
您需要改用for...in循环:
for (var key in req.body) {
if (req.body.hasOwnProperty(key)) {
item = req.body[key];
console.log(item);
}
}

