I have a static method in my UnitSchema that finds a unit by a token. It gets passed a token as a parameter and tries to find the unit with a normal findOne method on the Unit model:
UnitSchema.statics.findByToken = async function (token) {
const Unit = this
let unit = await Unit.findOne({ 'Tokens.token': token }).populate('Organisation')
return unit
}
I have had this method for a long time and it has always returned the correct object. Now, which seems like all of a sudden for no reason, it returns a "complete" mongoose object with all of the extra information and functions, and all the object ID's are now in an object format instead of a string format, such as:
ObjectID {
_bsontype: 'ObjectID',
id: <Buffer 5c 85 43 16 f1 ad 70 d8 f8 97 48 78>
}
instead of:
_id: '5d49595246853f14fc5168e9'
Because of this I can no longer populate fields with the normal: .populate('field') and this breaks my code.
I have searched around like crazy but can not find an explanation for this, and therefore I do not know how to fix it. Help is kindly appreciated!
I know this is a late answer. But I faced this issue recently and did some search on why the response is given in an Array of Buffer instead of an id. But I couldn't find any resource regarding it.
But, to convert the Buffer into an id. Just use String or toString().

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With