Я использую node.js получить пост запрос, тело запроса имеет это содержимое после его печати с помощью console.log()
:req.body не может быть прочитан как массив
{
'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'
}
}
я получаю запрос, используя приведенную ниже 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);
});
});
Вторая часть кода, которая имеет функцию forEach
дает это исключение «[TypeError: Object #<Object> has no method 'forEach']
»
ли вы, знаете, что мне не хватает?
возможный дубликат [Loop через объект JavaScript] (http://stackoverflow.com/questions/684672/loop-through-javascript-object) –