导出函数中Node.js中响应正文的未定义值。

如果

我有一条通过某条路线访问的控制器

router.post('/human',human.create);

人是通过其控制器文件导入的,该文件包含以下内容:

var config = require(__dirname + '/../config/config'),
logger = require(__dirname + '/../lib/logger'),
util = require(__dirname + '/../helpers/util');

exports.create = function(request, response, next){


response.send({

  id: "human_103tsG2eZvKYlo2CW5yEDyUe",
  firstName: "Patricia",
  middleName: null,
  lastName: "Cesar",
  sex: "Female",
  birthdate: null

});

};

在此创建函数中,我可以执行

console.log(request);

一个巨大的JSON对象将出现在终端中,包括我需要的属性:body。

但是,当我这样做时console.log(request.body),它变得不确定。

我是否缺少需要编码的Node或Express的特定功能?

James Jiang

我猜您正在使用Express 4.x,它解耦了许多中间件软件包,如@shredmill所提到的,您应该使用

bodyParser = require ('body-parser')

...

app.use(bodyParser.json()); //i found this works for me comparing to use bodyParser()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章