Home > Blockchain >  Mongoose `findById` not returning expected document
Mongoose `findById` not returning expected document

Time:10-29

When I find a document using the following line:

const user = await User.findById(req.user.id).populate('input')

And then I console.log user, I can see the document that fulfills the query.

However, when I add .updateOne({ formResponded: '1' }); to the previous line like so:

  const user = await User.findById(req.user.id).populate('input').updateOne({ formResponded: '1'});

and I console.log user I do not get any record but instead an object that looks like this:

{ n: 1,
  nModified: 1,
  opTime: {
    ts: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1635212112 },
    t: 41
 },
 electionId: 7fffffff0000000000000029,
 ok: 1,
 '$clusterTime': {
   clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1635212112 },
   signature: { hash: [Binary], keyId: [Long] }
 },
 operationTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1635212112 }
} 

Why does this happen?

CodePudding user response:

Fixed:

   const user = await User.findByIdAndUpdate({ _id: req.user.id }, { formResponded: '1' }).populate('input')
  • Related