Express GET route will not work with parameters Express GET route will not work with parameters express express

Express GET route will not work with parameters


It looks like you're trying to retrieve this URL:

/api/host/ama?id=SOMEID

However, you have a route declared for URL's that look like this:

/api/host/ama/SOMEID

In other words, the id is part of the path of the URL, and not passed as a query string parameter (that's what /:id means: it's a placeholder for a part of the URL that the route should match).

So either change the request-URL by adding the id to the path (/api/host/ama/58e395a8c6aaca2560089c‌​e7), or rewrite your route handler to something like this:

router.get('/ama', function (req, res, next) {    Ama.findById(req.query.id).then(function(Ama){        res.send(Ama);    });});

However, I would advise using the former (making the id part of the URL).


There are two problems here:

router.get('/ama/:id', function (req, res, next) {    Ama.findById(req.params.id).then(function(Ama){        res.send(Ama);    })    res.send(req.params.id)*/});

First of all, res.send(req.params.id) will run before the res.send(Ama); and it will probably send the entire response. You are missing the .exec() method call (see the answer by Cédric De Dycker for details). The res.send() can only be reliably used once. Maybe you want res.write() instead if you want to wrte multiple things. Also you don't handle the promise rejection so you don't handle database errors. To know why you should always handle the promise rejections see this answer: Should I refrain from handling Promise rejection asynchronously?


The parameter should work fine but it seems you missed to add .exec to your query

Ama.findById(req.params.id).exec().then(function(Ama){      res.send(Ama); })