I am asserting somewhere in mocha as :
assert.equal(model.organizationId,objId);
But i am getting the failed mocha result as :
Uncaught AssertionError: "5225777180a843d901000012" == "5225777180a843d901000012"
why it happens, when the id's are same.
And how to overcome it?
So mongodb ObjectIds in javascript are sadly frustrating in this regard. They are distinct objects resulting in obj1 === obj2
evaluating to false even though the value they represent is identical. There are 3 options:
Convert to strings before comparing. I use this a lot because it's so frustrating otherwise.
Use the .equals()
method they provide: assert.ok(model.organizationId.equals(objId))
Write a custom comparison function that can take null, ObjectIds, or Strings and do the right thing (I've also done this)
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