Monday, 5 June 2017

GET request for specific id returning null

I'm attempting to build a simple CRUD application using express and mongodb. My GET request for all database entries is working and my POST request is working but I can't seem to figure out the problem with my GET request for individual entries.

Server.js GET request:

app.get('/api/:id', function (req, res) {
  var id = req.params.id;

  db.collection('api').findOne({_id: id}, (err, result) => {
    if (err){
      res.sendStatus(404);
      return console.log(err);
    }
    res.redirect('/');
    return console.log(result);

  });

});

When I type 'url/api/593555074696601afa192d7f' which is an ID I know exists the console.log is returning null, I'm not sure if I'm doing something wrong?



via Matt

No comments:

Post a Comment