Thursday, 10 October 2019

FindOne not working

I'm fairly new to Mongoose, and having an issue finding a document by an ObjectId. Given the following fields in my document, when performing a findOne, it's not finding the document which I can clearly see exists in the db. What am I doing wrong?Please Note: I'm trying to find by userId, not _id._id:ObjectId("5d9f2f0b4bd8a42b18cbc146")userId:ObjectId("5d9e70e795f17227df2007fe")My code below seems to be creating a new document in the profiles collection every single time. Note that req.body.userId is "5d9e70e795f17227df2007fe".User.findOne({ userId: req.body.userId }).then((doc) => {if (doc) {console.log('doc found', doc)} else {console.log('doc not found')const profile = new Profile({userId: req.body.userId,company: req.body.company,phone: req.body.phone,website: req.body.website}).save().then(userProfile => res.json(userProfile)).catch(err => console.log(err));}}).catch( (err) => {console.log('Error: ', err)})

Submitted October 10, 2019 at 02:42PM by NetBoxter

No comments:

Post a Comment