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

199
Visualizações
Create a button that DELETES an item from MongoDB (Mongoose Node.js express.js)

I have a CRUD project where a user can insert data with forms and it's all stored in mongoDB. Everything seems to work fine but I can't find a solution on how to create a button that deletes a specific item from a DB.

//Code for Delete button (which is within the article when you click on it)

<form action="/delete/:id" method="post">
<button type='submit' class="btn btn-lg">Delete</button>
</form>

//This is my solution so far

app.post('/delete/:id', (req, res) => {
  Post.deleteOne((err) => {
    if (!err) {
      res.redirect('/')
    }
  })
});

So the problem is when I click Delete, it deletes the first item from the DB. I need it to delete the page where I clicked the button on.

before Delete ||| after Delete

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First things first, check if a provided ID is the actual object ID then you must pass that ID into the command:

app.post('/delete/:id', async (req, res) => {
  await Post.deleteOne({_id: req.params.id})
  return res.redirect('/')
});
about 4 years ago · Juan Pablo Isaza 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