I have a problem combining UnderscoreJS and MongooseJS in NodeJS. I have a result of mongoose and I want filter a array
var __ = require("underscore"),
platformInfo = __.findWhere(user.platforms, {"pId": platformId});
But pId inside user.platforms is a ObjectId and can't find. But if i make a each and compare like this all its OK:
__.each(user.platforms, function(platform){
if(platform.pId.toString() == platformId){
}
});
How i can find in findWhere method (one line, and cool) the same result? Thanks
Sadly mongodb ObjectId instances do not work properly with JavaScript's equality operators == or ===. You either need to use the provided method: objectId1.equals(objectId2) or ensure they are both converted to strings and then underscore or === will work.
platformInfo = _.filter(user.platforms, function (platform) {
return platform.pId.toString() === platformId;
})
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