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

238
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

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