I am trying to get my head around what is going here. I am search for a matching id using .findIndex()
(I am using mongoose). Despite having the exact same output and type, and using ==
I still return false. However, if I convert both using .toString()
it works. Unfortunately typeof
does not give me any more information than that both are objects.
Does anyone know why this is happening?
Code
moduleids = Object.values(user.modules.map(a=>[a.id]));
moduleindex = moduleids.findIndex(function(e) {
console.log("e: " + e);
console.log("module._id: " + module._id);
console.log("typeof module._id: " + typeof(module._id));
console.log("typeof e: " + typeof(e));
return(e == module._id);
});
Portion of user.modules schema.
modules: [ {
id: { type: Schema.Types.ObjectId, ref: 'Modules' },
modality: { type: String },
selected: { type: Boolean, default: false }
} ]
The module
variable is derived from a Modules schema.
Output
e: 5a4ee5d7e10c0a20dcb630e0
module._id: 5a4ee5d7e10c0a20dcb630e0
typeof module._id: object
typeof e: object
false // This only works if converted to string return(e.toString() == module._id.toString());.