req.body不能作为数组读取

时间:2015-02-27 12:10:23

标签: node.js

我正在使用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' 
   } 
}

我正在使用以下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']

你知道我错过了什么吗?

3 个答案:

答案 0 :(得分:13)

req.body不是数组,而是具有两个属性的对象。从您提供的console.log输出中可以看出这一点。因此,它没有length属性,也没有forEach方法。

如果它是一个数组,它看起来像这样:

[ 
   { 
     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' 
   } 
]

要遍历对象的键,可以使用构造

for(var key in req.body) {
  if(req.body.hasOwnProperty(key)){
    //do something with e.g. req.body[key]
  }
}

答案 1 :(得分:9)

forEach仅针对数组定义。

您需要使用for...in循环:

for (var key in req.body) {
  if (req.body.hasOwnProperty(key)) {
    item = req.body[key];
    console.log(item);
  }
}

答案 2 :(得分:1)

我使用 JS 对象,它工作得很快而且肯定。此外,我将 Object.keys 重新用于电子邮件/数据库行/列名称。这是工作网站 (professorMiriam.com/donate) 代码:

      app.post('/donate', (req, res) => {  
    let nm1=req.body.name1,nm2=req.body.name2;
    let frm={
    'Name':nm1 +' '+ nm2,
    'Email':req.body.email,
    'Date':req.body.dt,
    'Endorse':req.body.rcmmnd,
    'Note':req.body.note,
    'ip':req.body.ip,
    'Amount':req.body.mnt,
    'Often':req.body.often};
    
     for(const key of Object.keys(frm)) {if(frm[key].length<1){frm[key]='N/A';}}

//simplicity to add email just in 2!! lines:
let strng=JSON.stringify(frm); strng=strng.replace(/","/g,'<br>').replace(/":"/g,': ').replace('{"','').replace('"}','<br>END');    
var mail={from:'steve@websystem.us',to:[USR,USRF],subject:usd,html:strng};