find filter issue

47 views
Skip to first unread message

ashcs...@gmail.com

unread,
Sep 28, 2017, 7:26:27 AM9/28/17
to LoopbackJS
Hi All,

I have two models named Customer and Profiles. A customer can have more than one profile. I am using mongodb as a datasource. I am storing id from the Customer model in the Profile model as customer-id. But trying to find it does not work.

profile.json:

{
 
"name": "profile",
 
"plural": "profiles",
 
"base": "PersistedModel",
 
"idInjection": true,
 
"options": {
   
"validateUpsert": true
 
},
 
"properties": {
   
"customer-id": {
     
"type": "string"
   
},
   
"member-id": {
     
"type": "string"
   
},
   
"display-name": {
     
"type": "string"
   
}
 
},
 
"validations": [],
 
"relations": {
   
"albums": {
     
"type": "embedsMany",
     
"model": "album",
     
"property": "albumslist",
     
"foreignKey": "id",
     
"options": {
       
"validate": true,
       
"forceId": false
     
}
   
}
 
},
 
"acls": [
   
{
     
"accessType": "*",
     
"principalType": "ROLE",
     
"principalId": "$unauthenticated",
     
"permission": "DENY"
   
}
 
],
 
"methods": {}
}


Profile.find(
               
{ where: { 'customer-id': '59ccd23ed23cf02ac4fa3a07' } },
               
function (error, profiles) {
                   
if (error) {                        
                   
}
 
 callback
(null, profiles);
 
});

It works if we try to find using member-id or id, but not with customer-id. It never works with customer-id. Could anyone let me know what exactly may be the issue here ?

Thanks

ashcs...@gmail.com

unread,
Sep 28, 2017, 7:50:45 AM9/28/17
to LoopbackJS
Looks like there s a problem while searching using the generated id. Any ideas anyone ?

Francois Laforge

unread,
Oct 2, 2017, 11:02:46 AM10/2/17
to LoopbackJS
The problem is your customer id is being stored as a string in your database, but loopback is converting it to a BSON type, and BSON !== string.

Loopback detects BSON types by testing if it satisfies a RegEx of 24 alpha-numeric characters.  If it does, then your '59ccd23ed23cf02ac4fa3a07' is being converted to a BSON.

We had this problem too.  3 solutions:

(1) save your csutomer_id as a BSON type, not a string

(2) Add an underscore to your customer_id to break the RegEx:   '_59ccd23ed23cf02ac4fa3a07'.  This solution isn't the best because you then have to override every CRUD function to append the underscore to the string

(3) add your customer Id as a relationship:

"relations": {
 
"customer":{
   
"type": "belongsTo",
   
"model": "User",
   
"foreignKey": "customer_id"
 
}

 
"member":{
   
"type": "belongsTo",
   
"model": "User",
   
"foreignKey": "member_id"
 
}
}


Matt Ober

unread,
Dec 26, 2017, 5:22:42 PM12/26/17
to LoopbackJS
@Francois Laforge

You mention saving the customer id as a BSON? Could you explain how to do this? I'm running into the same issue as the opening poster, but when I switch my serverId type to "BSON", loopback doesn't recognize the BSON type at runtime.

Matt Ober

unread,
Dec 27, 2017, 10:50:25 AM12/27/17
to LoopbackJS
For those of you still struggling with this, we found out that if you add

"options": {
 
"strictObjectIDCoercion": true
},

to your model's json file, then loopback won't automatically change the ID to BSON, therefore solving the problem.
Reply all
Reply to author
Forward
0 new messages