Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

281
Visualizações
Express GET route will not work with parameters

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.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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?

over 4 years ago · Santiago Trujillo Relatório

0

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);
 })
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda