These are chat archives for Automattic/mongoose

29th
Jul 2018
Jonathan Díaz
@SKEPDIMI
Jul 29 2018 01:29
I'm back @lineus! with a strange issue...

So, my client-side is creating a get request for a post.
I will the use my Post_model to findOne post with the id provided

  Post_model.findOne({_id: postId})
  .then(post => {
    callback(200, post, 'application/json')
  })
  .catch(err => {
    callback(404, {message: 'Could not find post'}, 'application/json')
  });

The issue is...
.then will give me null..

The id is correct, I have triple checked, and even if it were wrong, wouldn't Mongoose run catch instead of then ???

Jonathan Díaz
@SKEPDIMI
Jul 29 2018 01:40
It also works with findById!
// this worked
Post_model.findById(postId)
    .then(post => {
      ...
Am I missing out on something ?
Damien Marble
@ThisIsNoZaku
Jul 29 2018 03:05
Is postId a string?
Damien Marble
@ThisIsNoZaku
Jul 29 2018 03:11
@SKEPDIMI in the docs, it says findById casts internally, so what may be happening is that when you're passing a string to find, nothing matches because it's comparing a string to an ObjectId and the types don't line up.
Jonathan Díaz
@SKEPDIMI
Jul 29 2018 16:02
@ThisIsNoZaku I am passing a string to find! This might be it, yes. I will try using _id: ObjectId("idstringhere")
flkeysils
@flkeysils
Jul 29 2018 17:55
@campanagerald . Oh thanks i just had to remove a statement. thanks!