req.body can't be read as an array
req.body
isn't an array, but an object with two properties. This is evident from the console.log output you've provided. Therefore, it has no length
property and no forEach
method.
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]
}
}
forEach
is defined only for Arrays.
You need to use for...in
loop instead:
for (var key in req.body) {
if (req.body.hasOwnProperty(key)) {
item = req.body[key];
console.log(item);
}
}