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

317
Visualizações
how to update by using id in express js and mongoose with parameters

This is how my data look from frontend from mongodb

this is my ejs code which on clicked on update uses the item.id on url

<div class="header">
    <a href="./viewdatabase">Get Details in json format</a>
    <a href="./getdetails">refresh</a>
</div>
<hr>

<% if(details!=null) { %>
    <table id="bordertable">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Gender</th>
            <th>Locality</th>
            <% details.forEach((item)=>{ %>
        <tr>
            <td>
                <%= item.name%>`enter code here`
                <a href="/update/<%= item._id %>" >update</a>
            </td>
            <td>
                <%= item.age %>
            </td>
            <td>
                <%= item.gender%>
            </td>
            <td>
                <%= item.locality%>
            </td>

        </tr>
        <% }) %>
    </table>
    <% } %>

my index.js

    app.get('/update/:id', (req, res) => {    
    var id = req.params.id; 
       res.render('edit.ejs');  
       console.log(req.params.id);
    });

    app.post('/edit',(req, res) => {
     gymApplicant.updateOne({ _id:req.params.id }, {$set: {
        age: req.body.age,
        gender:req.body.gender
     }
     }, function (err, result) {
          if (err) {
          console.log(err);
        } else {
         console.log("Post Updated successfully");
         res.render('abc.ejs');
     }
    });
    });

which on click on update it redirects to a form to update the age but when i send post request it says not defined because id is out of scope so any idea how to solve this issue.

Thanks in advance

about 4 years ago · Juan Pablo Isaza
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