我在这行代码中得到了“类型错误:无法读取未定义的属性”

时间:2016-03-23 01:47:34

标签: javascript node.js curl faye

我正在尝试测试我通过faye从客户端发送的消息。

curl -X POST -H "Content-Type:application/json" -d '{"message":"Hi there."}' http://localhost:8000/message

这是culript系列。

TypeError: Cannot read property &#39;message&#39; of undefined<br>

更新:culript实际上可能在这里......

app.post('/message', function(req, res) {
  bayeux.getClient().publish('/channel', {text: req.body.message});
  res.send(200);
});

不幸的是,我收到了这个错误。出于某种原因,它将消息视为未定义的属性,我不确定原因。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您缺少必需的正文解析中间件,例如body-parser.json()中间件。

安装该模块并添加

var bodyParser = require('body-parser');
app.use(bodyParser.json());

在路线前的某个地方。