mongoose
mongoose copied to clipboard
Compare _id with an ObjectId in a different document crashes
So I tried to do the following:
client is a mongoose document.
user is a mongoose document with a clientId property in the schema as ObjectId.
The client id is the same as user.clientId in the database.
Both are retrieved using a find method from mongoose.
client._id.equals(user.clientId); // CRASHES
I did some debugging and it seems that _id is a BSON ObjectID but clientId is a mongoose.Types.ObjectId which does not have a toHexString method to allow the BSON ObjectId to perform equals properly.
Here is the BSON ObjectID code where it crashes:
/**
* Compares the equality of this ObjectID with `otherID`.
*
* @param {Object} otherID ObjectID instance to compare against.
* @return {Bool} the result of comparing two ObjectID's
* @api public
*/
ObjectID.prototype.equals = function equals (otherID) {
var id = (otherID instanceof ObjectID || otherID.toHexString)
? otherID.id
: ObjectID.createFromHexString(otherID).id;
return this.id === id;
}
Is this a bug?
My bad in my code it was a spelling mistake of clientID instead of clientId so it was undefined.