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

239
Vistas
Unable to update a document using save()

I am working on a NodeJS project. I am using express and mongoose. I am trying to update a document in MongoDB but updates are not showing up. Also, there is no error as the page is getting redirected as per res.redirect('/content/contnet-view/'+id)

router.post('/posthere', function(req, res){
  const { id, Status, ModuleIndex, ItemIndex } = req.body;
   Content.findOne({_id: id}, function(err, foundContent){
   
if(err){
console.log(err);
res.status(500).send();
}else{
if(!foundEnroll){
res.status(404).send();
}else{
if(id){
  foundContent.ContentData.ContentM[ModuleIndex].ContentI[ItemIndex].Status = Status;
}
foundContent.save(function(err, updateContent){

  if(err){
    console.log(err);
    res.status(500).send();
  }else{
      
      
    res.redirect('/content/contnet-view/'+id) 
  }
})}}
  })})

Any idea what I am doing wrong here?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Using findByIdAndUpdate is better approach but in this case, mongoose save function returns a promise object. If save() succeeds, the promise resolves to the document that was saved

doc.save().then(savedDoc => {
       savedDoc === doc; // true
       });

You need to use thenable chain approach to handle async function instead of callback

about 4 years ago · Juan Pablo Isaza Denunciar

0

It was an issue with misconfigured Schema. matching it with the Document format fixed it.

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