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

193
Visualizações
Why is my Update fails to address a field even though it finds the right document?

here is an example of a JSON file of a document

{
  _id: new ObjectId("6212fc135188d701253762a2"),
  title: 'How do i make a lemon?',
  text: '<p>please im confused?????</p>',
  authorUsername: 'SweetWhite',
  dateCreated: 2022-02-21T02:42:27.837Z,
  answers: [
    {
      authorUsername: 'SweetWhite',
      text: '<p>YOU GOTTA DO THE RAIN DANCE YEAH AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</p>',
      date: 2022-02-21T02:42:42.785Z,
      replies: [],
      likes: [Array],
      dislikes: [Array],
      reports: [Array]
    }
  ],
  likes: [ 0 ],
  dislikes: [ 0 ],
  tag: 'History',
  views: [ 1, 'SweetWhite' ],
  reports: [ 'SweetWhite' ],
  checked: false,
  reportsNested: [ { username: 'SweetWhite', answerIndex: 0 } ],
  __v: 0
}

and here is my query for the update with the router:

router.post('/reportsNested', 
  body('nestedReportObj'),
  body('questionTitle'),

  function(req,res,next){
  console.log(req.body.questionTitle);
  console.log(req.body.nestedReportObj) 
  Question.updateOne({title: req.body.questionTitle},
    {$pull: {reportsNested: req.body.nestedReportObj}},
    function(err, result){
      if(err) {return next(err)}
      console.log(result)  
    }
  )  
  res.redirect(req.get('referer'))
})

the console logs print out

How do i make a lemon?
{"username":"SweetWhite","answerIndex":0}
{
  acknowledged: true,
  modifiedCount: 0,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}

the first two console logs print okay values, they find the right document using the title and also they print the right value we want to pull.

from the last log it seems it found the right document, but did not update anything, why is that? in my router I am pulling a value from the reportsNested, which seems to be a field in the JSON file, why is it not pulling anything then? what am I doing wrong?

Thanks!

about 4 years ago · Juan Pablo Isaza
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