Issues when adding relations

96 views
Skip to first unread message

Mowso

unread,
Apr 25, 2016, 9:35:40 AM4/25/16
to loopb...@googlegroups.com
I have a Loopback API in which I have a container and I'm trying to add some relations to my models (using this) but when it comes to adding relations to container.js, it does not work anymore and I get this error :

/home/catalyse/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:434
          modelClass[relation.type].call(modelClass, name, params);
                                   ^


TypeError: Cannot read property 'call' of undefined
    at EventEmitter.<anonymous> (/home/catalyse/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:434:36)
    at EventEmitter.g (events.js:260:16)
    at emitOne (events.js:77:13)
    at EventEmitter.emit (events.js:169:7)
    at DataSource.setupDataAccess (/home/catalyse/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:540:14)
    at DataSource.attach (/home/catalyse/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:752:8)
    at Function.ModelClass.attachTo (/home/catalyse/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/model-builder.js:336:16)
    at Registry.configureModel (/home/catalyse/Documents/loopback-examples/test/node_modules/loopback/lib/registry.js:238:15)
    at configureModel (/home/catalyse/Documents/loopback-examples/test/node_modules/loopback/lib/application.js:441:16)
    at EventEmitter.app.model (/home/catalyse/Documents/loopback-examples/test/node_modules/loopback/lib/application.js:133:5)



I have three models like in the example I link above, they basically just have different names. Here is my container.json :

   
 {
     
"name": "Container",
     
"base": "PersistedModel",
     
"properties": {
       
"name": {
         
"type": "string"
       
}
     
},
     
"validations": [],
     
"relations": {
         
"user": {
         
"type": "belongsTo",
         
"model": "user",
         
"foreignKey": "ownerId"
       
}
     
},
     
"acls": [],
     
"methods": {}
   
}


and my user.json :

   
{
     
"name": "user",
     
"base": "User",
     
"idInjection": true,
     
"options": {
       
"validateUpsert": true
     
},
     
"properties": {},
     
"validations": [],
     
"relations": {
       
"containers": {
         
"type": "hasOne",
         
"model": "Container",
         
"foreignKey": "ownerId"
       
},
       
"companies": {
         
"type": "hasOne",
         
"model": "company",
         
"foreignKey": "ownerId"
       
}
     
},
     
"acls": [],
     
"methods": {}
   
}



Here is the model-config.json :

   
{
     
"_meta": {
       
"sources": [
         
"loopback/common/models",
         
"loopback/server/models",
         
"../common/models",
         
"./models"
       
],
       
"mixins": [
         
"loopback/common/mixins",
         
"loopback/server/mixins",
         
"../common/mixins",
         
"./mixins"
       
]
     
},
     
"User": {
       
"dataSource": "db"
     
},
     
"AccessToken": {
       
"dataSource": "db",
       
"public": false
     
},
     
"ACL": {
       
"dataSource": "db",
       
"public": false
     
},
     
"RoleMapping": {
       
"dataSource": "db",
       
"public": false
     
},
     
"Role": {
       
"dataSource": "db",
       
"public": false
     
},
     
"Container": {
       
"dataSource": "myfile",
       
"public": true
     
},
     
"user": {
       
"dataSource": "db",
       
"public": false
     
},
     
"company": {
       
"dataSource": "db",
       
"public": false
     
}
   
}


and finally my datasource.json, notice that I have a storage folder at the root of my project :

   
 {
     
"db": {
       
"name": "db",
       
"connector": "mongodb"
     
},
     
"myfile": {
       
"name": "myfile",
       
"connector": "loopback-component-storage",
       
"provider": "filesystem",
       
"root": "storage"
     
}
   
}


I have posted my issue on Stack Overflow but we did not find out what was wrong, any idea :) ?


benj...@snapbarapp.com

unread,
Jun 6, 2016, 9:09:01 AM6/6/16
to LoopbackJS
Hi Mosow ! 

I have the exact same problem here. 
Did you find a solution by yourself now ?
Thks by advance !

Mowso

unread,
Jun 23, 2016, 9:18:12 AM6/23/16
to LoopbackJS
Hi !

Finally an other project came up and I was not able to figure out how to solve this one, sorry :/
Anyhow if eventually I got back on the project and find the answer I would post it here but in the mean time..

Law Kingston

unread,
Jul 14, 2017, 11:21:34 AM7/14/17
to LoopbackJS
Hello,

I have exaclty the same problem.
Any solution?


Le lundi 25 avril 2016 15:35:40 UTC+2, Mowso a écrit :

Francois Laforge

unread,
Jul 14, 2017, 11:35:46 AM7/14/17
to LoopbackJS
Just some debugging questions that I'm sure you've already tried, but have you tried renaming your model from "user" to something else?  "Member" perhaps?

We have many models that "belongTo" our Member model which uses User as it's base model and have no problems.  

Also, the name of your relation is the same as your model.  Maybe try this:

      "relations": {
         
"owner": {
           
"type": "belongsTo",
           
"model": "Member",
           
"foreignKey": "ownerId"
       
}
Reply all
Reply to author
Forward
0 new messages