Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!