2016-05-16 30 views
0

http://sailsjs.org/documentation/concepts/models-and-orm/associations/through-associationsSailsjs通過關係請求格式

按照上述鏈接,我已經在3個模型「用戶」,「寵物」和「Pet_user」,然後我用下面請求插入記錄。但是,它插入用戶和寵物表而不是pet_user表。

{ 
    "name":"BB21", 
    "user_name":"BB21", 
    "email_personal" : "[email protected]", 
    "email_official" : "[email protected]",  
    "pets" : [ 
     { 
      "pet" : 1, 
      "owner" : 1 

     } 
    ] 
} 

//Controller  
create: function(req, res) { 
    User.create(req.params.all()).exec(function(err, user) { 
     if (err) 
      return res.json(402, err); 
     if(user) { 
      res.json('200', {message: sails.config.messages.USERCREATION}); 
     } 
    }); 
}, 
+0

請共享執行插入 –

+0

的代碼的其餘部分'code' //控制器 創建:功能(REQ,RES){ \t User.create(req.params.all 。())EXEC(函數(ERR,用戶){\t \t如果(ERR) \t返回res.json(402,ERR); \t \t 如果(用戶){\t \t \t \t \t \t \t res.json( '200',{消息:sails.config.messages.USERCREATION}); \t} \t}); \t}, –

+0

「所有者」鍵應該來自當前插入的ID。 –

回答

1
Id has to be included while using through in model collection 

{ 
"name":"BB21", 
"user_name":"BB21", 
"email_personal" : "[email protected]", 
"email_official" : "[email protected]",  
    "pets" : [ 
     { 
      "id" : 1 
     } 
    ] 
}