0

I have several post methods in node. One of them works, now I am working on the other. I've tried switching the order of the key-value pairs in the res.body, restarting my backend server, and a few other things, and now I am out of ideas. I am using postman so maybe an issue with that, but maybe you guys will see an obvious error that I don't.

Here's the model:

var UserGameSchema = new mongoose.Schema({
user: { type: String, required: true },
game: { type: String, required: true },
own: { type: Boolean, required: true }
});

var UserGame = mongoose.model('UserGame', UserGameSchema);

module.exports.UserGame = UserGame;

here's my method:

app.post('/mygames', function(req, res) {
UserGame.create({
    user: req.body.user,
    game: req.body.game,
    own: req.body.own
}, function(err, item) {
    if (err) {
        return res.status(500).json({
            message: 'Internal Server Error'
        });
    }
    res.status(201).json(item);


   });
});

and here is the res.body I am sending using Postman

{
"user": "Colin",
"own": true,
"game": "Grand Theft Auto 5 (PS4)"
}

Yes, I am pretty sure I am using the correct URL: http://localhost:8080/mygames

still getting this 500 error. Specifically postman is returning in the console:

TypeError: Cannot read property 'user' of undefined

Im not sure why user would be undefined.

Thanks for any feedback.

If looking for way too much info, here is the github repo for this: https://github.com/auldsy-ababua/gameswap

Colin Aulds
  • 147
  • 1
  • 3
  • 16
  • Are you using [`body-parser`](https://www.npmjs.com/package/body-parser)? – robertklep Sep 20 '16 at 08:55
  • I am using body-parser – Colin Aulds Sep 20 '16 at 08:55
  • Since `req.body` seems to be undefined, you may not be using it properly. For instance, it has to be declared before your routes. Also, you need to make sure that it it set up to parse JSON data (because that's what you're sending to it). – robertklep Sep 20 '16 at 08:59
  • That's interesting. My post method for creating new users works fine. ```var user = new User({ username: username, password: hash, city: req.body.city, email: req.body.email });``` – Colin Aulds Sep 20 '16 at 09:04
  • I'm only assuming that `req.body` is undefined and causing the error, but to be sure, try logging `req.body` inside your `/mygames` handler. – robertklep Sep 20 '16 at 09:06
  • hmm yeah it's logging undefined. so should I just create a var set to req.body? – Colin Aulds Sep 20 '16 at 09:11
  • No, you should try and find out why it's undefined. My guess is that you are loading `body-parser` _after_ the route to `/mygames` has been declared. – robertklep Sep 20 '16 at 09:12
  • hmm body parser is required up at the top of my server.js...and my other post method to create new users (above this post method) works fine. So strange. – Colin Aulds Sep 20 '16 at 09:15
  • `req.body` being undefined is a telltale sign of `body-parser` not having been called (otherwise, it would populate `req.body` with an empty object). Can you add your `body-parser` and route setup to your question? – robertklep Sep 20 '16 at 09:27

1 Answers1

0

You're only using body-parser for one specific route:

app.post('/users', jsonParser, function(req, res) { ... });

So it's not being used for the POST /mygames route. Either add it there as well, or add it globally:

app.use(jsonParser);

...your routes...
robertklep
  • 198,204
  • 35
  • 394
  • 381