有没有办法在sails.js控制器中从嵌入式json创建相关对象?
在正在创建的类的控制器中,我需要在另一个类中创建一个相关记录,并包含刚刚创建的父记录的id。
"孩子的数据"记录嵌入在创建父记录的json中。正在使用的数据库是Mongo,因此它不会强制执行关系。
如何创建这些相关对象?
非常感谢任何参考的例子。
更新:
我已经在第一个答案中实施了更改,现在我正在收到错误" [TypeError:无法调用方法'所有'未定义的]"
有什么想法吗?
编辑添加代码:
PurchaseInfo模型
module.exports = {
attributes: {
parent_account:{
type:'string'
},
purchase_date:{
type:'datetime'
},
product:{
type:'string'
},
subscription_length:{
type:'integer'
},
renewal_date:{
type:'date'
},
users_authorized:{
type:'integer'
},
users_activated:{
type:'integer'
},
company_name:{
type:'string'
},
company_contact:{
type:'string'
},
company_email:{
type:'string',
required:true,
email:true
},
company_phone:{
type:'string'
},
intended_use:{
type:'string'
},
// reference to company
company:{
collection: 'company',
via:'purchaser'
}
}
};
公司模式
module.exports = {
attributes: {
company_name:{
type:'string',
required:'true'
},
main_addr1:{
type:'string',
required:'true'
},
main_addr2:{
type:'string',
required:'true'
},
main_city:{
type:'string',
required:'true'
},
main_state:{
type:'string',
required:'true'
},
main_zip:{
type:'string',
required:'true'
},
main_country:{
type:'string',
required:'true'
},
mailing_same_as_main:{
type:'boolean'
},
mailing_addr1:{
type:'string'
},
mailing_addr2:{
type:'string'
},
mailing_city:{
type:'string'
},
mailing_state:{
type:'string'
},
mailing_zip:{
type:'string'
},
mailing_country:{
type:'string'
},
primary_contact:{
type:'string'
},
company_email:{
type:'string',
required:true,
email:true
},
purchaser: {
model: 'purchaseinfo'
}
}
};
PurchaseInfo控制器
module.exports = {
create: function(res, req){
var purchaseData = req.params.all();
// Extract the company info from the POSTed data
var companyData = purchaseData.company_info;
// Create the new Purchase record
PurchaseInfo.create(purchaseData).exec(function(err,newPurchaseInfo){
if (err){return res.req.serverError(err);}
// Create the new Company, linking it to the purchase
companyData.purchaser = newPurchaseInfo.id;
Company.create(companyData).exec(function(err, newCompany) {
if (err) {return res.serverError(err);}
return res.json(newPurchaseInfo);
})
})
},
_config: {}
};
POST
http://localhost:1337/purchaseinfo/create
body:
{ "parent_account": 1, "purchase_date": "2014-03-03", "product": "C V1", "subscription_length": 12, "renewal_date": "2015-03-03", "users_authorized": 10, "users_activated": 1, "company_name": "Bob's Trucking", "company_contact": "Jane Smith", "company_email":"jsmith@bobstrucking.com", "company_phone": 5035129657, "intended_use": "trucking", "company_info": { "company_name": "Bob's Trucking", "main_addr1": "101 First Street", "main_city": "Portland", "main_state": "OR", "main_zip": "97230", "main_country": "USA", "mailing_same_as_main":"true", "primary_contact": "Jane Smith", "company_email": "info@bobstrucking.com" } }
答案 0 :(得分:1)
Sails v0.10允许您在模型之间创建关联。以下是User
和Pet
模型的快速示例:
/* api/models/User.js */
module.exports = {
attributes: {
name: 'string',
pets: {
collection: 'pet',
via: 'owner'
}
}
};
/* api/models/Pet.js */
module.exports = {
attributes: {
name: 'string',
breed: 'string',
owner: {
model: 'user'
}
}
};
/* api/controllers/UserController.js */
module.exports = {
create: function(req, res) {
// Assuming the POST contains all the data we need to create a user...
var userData = req.params.all();
// Extract the pet from the POSTed data
var petData = userData.pet;
// Delete the pet from the POSTed data, if you
// don't want it saved to the User collection in Mongo
delete userData.pet;
// Create the new user
User.create(userData).exec(function(err, newUser) {
if (err) {return res.serverError(err);}
// Create the new pet, linking it to the user
petData.owner = newUser.id;
Pet.create(petData).exec(function(err, newPet) {
if (err) {return res.serverError(err);}
return res.json(newUser);
});
});
}
};
此代码包含对User的默认create
蓝图操作的覆盖;你也可以为它创建一个单独的控制器动作。实际上,您也可能想要检查宠物是否已经存在,进行其他验证等等。但这应该可以让您继续前进!