Express GET route will not work with parameters

2019-06-22 18:30发布

问题:

I am new to Express and Mongoose. I am currently working on my first project, that is not a tutorial, and I has run into a problem.

I have multiple routes, they are defined in the index.js like this:

app.use('/api/client',require('./routes/client'));
app.use('/api/host',require('./routes/host'));

In the routes, there are multiple verbs that work, like PUT and POST. Here is the problematic route (I am trying to do more that what is presented here, but what is presented here, does not work as well):

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

This should work, right? It should return the document in the database, with that id. And I have checked if the document excists, probably around a 100 times. Now, if I simplify the route greatly, removing the id, and make a simple response, the route works:

router.get('/ama', function (req, res, next) {
    res.send({type:"GET"});
});

It's so wierd, that as soon as i add the parameter, i get a:

<pre>Cannot GET /api/host/ama</pre>

in Postman.

Any ideas? Mongod is running, my other routes are working.

回答1:

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).



回答2:

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?



回答3:

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);
 })