如何在Sails js中处理嵌入的json
How to handle embedded json in Sails js
是否有办法在sails.js控制器中从嵌入式json创建相关对象?
在正在创建的类的控制器中,我需要在不同的类中创建一个相关的记录,并包含刚刚创建的父记录的id。
"子"记录的数据嵌入在创建父记录的json中。使用的数据库是Mongo,所以它不强制关系。
如何创建这些相关的对象?
如能举例说明,不胜感激。
更新:我已经在第一个答案中实现了更改,现在得到错误"[TypeError: Cannot call method 'all' of undefined]"
任何想法?
编辑添加代码:
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: {}
};
文章
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" } }
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);
});
});
}
};
这段代码包括覆盖默认的create
蓝图动作为用户;您还可以为它创建一个单独的控制器动作。在现实中,您可能还想检查宠物是否已经存在,进行其他验证等等。但这样你就能上路了!
相关文章:
- 处理JSON字符串会导致JavaScript错误
- 处理一个JSON文件;完全相同的副本不是
- 通过用于气泡图的PHP将SQL处理为JSON
- 使用JavaScript处理JSON数据
- 如何在phonegap应用程序中处理Ajax json响应
- JSON.stringify没有'在更改的对象上无法按预期工作.如何处理
- 在谷歌应用引擎上用jQuery处理json表单
- JSON无法处理OnChange事件
- 使用JavaScript对JSON对象进行迭代无法处理此数据
- Ajax/JSON请求php处理页面
- XML 到 JSON - 处理 JavaScript 中的 xml 命名空间和别名
- JSON 处理 - 向 JSP 发送 JSON 文本字符串,如何在 JSP 中处理
- 杰克逊 JSON 处理密钥名称 = ognl 名称
- 复杂 JSON 处理期间的“范围错误: 超出最大调用堆栈大小”
- json对象内部的json处理不正确
- Mootools类,json处理/函数未定义
- JSON:处理textArea中的换行符(和其他无效的JSON字符)
- 数据没有通过JQuery Ajax (JSON)处理到php表单处理器
- JavaScript json处理程序
- 字符串常量和 JSON 处理