Search code examples
javascriptnode.jscurlfaye

I'm getting a "Type Error: Cannot read property of undefined" in this line of code


I'm trying to test a message I am sending from a client through faye.

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

This is the culript line.

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

UPDATE: The culript might actually be here...

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

Unfortunately, I get this error. For some reason, it sees message as an undefined property,and I'm not sure why.

Thanks in advance for your help.


Solution

  • You're missing the required body-parsing middleware, such as body-parser's .json() middleware.

    Install that module and add

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

    somewhere before your routes.