I am trying to make a query in a loopback model JS file. It is very simple and it as follow:
//isAdmin function: gets a boolean if a given userProfileId is an admin or not.
Userprofile.isAdmin = function(userProfileId, cb) {
let role = app.models.Role;
let rolemapping = app.models.RoleMapping;
let filter = {
"where": {
"name": "admin"
},
"fields": ["id"]
};
role.findOne(filter, function(err, instance) {
let filter2 = {
"where": {
"roleId": instance.id
,
"principalId": userProfileId,
},
"fields": ["id"]
};
rolemapping.find(filter2, function(err, instance) {
if (instance != "") {
cb(null, true);
} else {
cb(null, false);
}
});
});
};
This works perfectly in our development server. The console.log
of filter2
, in that server returns:
{ where:
{ roleId: 5890ef8bbef9b73e568c6933,
principalId: '5890ef8bbef9b73e568c6932' },
fields: [ 'id' ]
}
}
The console.log
of instance.id
looks like:
ObjectID { _bsontype: 'ObjectID', id: 'Xï¾ù·>Vi3 }
The issue is in our production server. I have made the deployment process and the result is slightly different, but it does not work at all :(.
The console.log
of filter2
returns:
{ where:
{ roleId: 58921dff16d9a37009e21104,
principalId: '5890ef8bbef9b73e568c6932' },
fields: [ 'id' ] }
}
And, console.log(instance.id)
returns:
ObjectID { _bsontype: 'ObjectID', id: Buffer [ 88, 146, 29, 255, 22, 217, 163, 112, 9, 226, 17, 4 ] }
This query in our production server does not return any documents, even if there are documents in the DB that satisfy the query.
I have compared all the npm packages, and all are exactly the same version. As well the servers (we are using Debian 8) and mongo (v3.2.10).
Does any have an idea to resolve this?
Thanks in advance!
Pedro.
I have experienced a similar issue when querying with an ID of a related model. Solved the issue by opting for like instead of equivalence like below:
{where:
{and:[{roleId: {like:'.*'+roleIdVar+'*.', options:'i'}},
{principalId: {like:'.*'+principalIdVar+'*.', options:'i'}}]
}
},
{fields: [ 'id' ] }
Please also note that field projection should be a separate object from the where filter.