Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda