Addding an object to a hasManyTrough relation on loopback

22 views
Skip to first unread message

PJEstrada006

unread,
May 29, 2016, 2:21:27 PM5/29/16
to LoopbackJS
I have this three models:


   
{
     
"name": "ExeboardUserBoard",
     
"base": "PersistedModel",
     
"idInjection": true,
     
"options": {
       
"validateUpsert": true
     
},
     
"properties": {},
     
"validations": [],
     
"relations": {
       
"exeboardUser": {
         
"type": "belongsTo",
         
"model": "exeboardUser",
         
"foreignKey": "exeboardUserId"
       
},
       
"board": {
         
"type": "belongsTo",
         
"model": "board",
         
"foreignKey": "boardId"
       
},
     
},
     
"acls": [],
     
"methods": {}
   
}



Model: ExeboardUser


    {
     
"name": "ExeboardUser",
     
"base": "User",
     
"idInjection": true,
     
"options": {
       
"validateUpsert": true
     
},
     
"properties": {
       
"name": {
         
"type": "string",
         
"required": true
       
}
     
},
     
"validations": [],
     
"relations": {
       
"boards": {
         
"type": "hasMany",
         
"model": "Board",
         
"foreignKey": "exeboardUserId",
         
"through": "ExeboardUserBoard"
       
}
     
},
     
"acls": [],
     
"methods": {}
   
}





Model: Board



   
{
     
"name": "Board",
     
"plural": "Boards",
     
"base": "PersistedModel",
     
"idInjection": true,
     
"options": {
       
"validateUpsert": true
     
},
     
"properties": {
       
"name": {
         
"type": "string",
         
"required": true
       
}
     
},
     
"validations": [],
     
"relations": {
       
"users": {
         
"type": "hasMany",
         
"model": "ExeboardUser",
         
"foreignKey": "boardId",
         
"through": "ExeboardUserBoard"
       
}
     
},
     
"acls": [],
     
"methods": {}
   
}




I´m trying to prepopulate my db with a board having a single user. However I´m not able to access the `board.users` property. It gives me the error: 

    TypeError: Cannot read property 'type' of undefined

This is the code I have


   
var users = [
       
{
          name
: "test"
   
       
}
     
];
     
//Create Board
     
function createBoard(cb){
        pgdb
.automigrate('Board', function(err) {
         
//if (err) return cb(err);
         
var Board = app.models.Board;
         
Board.create([
           
{name:"Board 1"
           
},
           
{name:"Board 2"
           
}
   
           
],function(err,boards){
              boards
[0].users.add(users[0]);
   
         
});
   
       
});
   
       
//Create ExeboardUser
     
function createExeboardUser(cb){
        pgdb
.automigrate('ExeboardUser', function(err) {
         
//if (err) return cb(err);
         
var ExeboardUser = app.models.ExeboardUser;
           
ExeboardUser.create(users,function(err,users){




           
});
       
});
     
}


Ebrahim Pasbani

unread,
May 30, 2016, 1:23:40 PM5/30/16
to LoopbackJS
You should create `users` in loopback system. You consider it as a plain object.
`boards[0].users.add()` take an object with type `ExeBoardUser`
Reply all
Reply to author
Forward
0 new messages