node.js - Deep associations in sails mongo using populate method? -
i new sails.js , using "sails.js mongodb". having problem deep associations using populate in sails app.
i have relationship this:
category has many many relationship article. city has 1 many relationship areas. article has 1 one relationship city , areas.
category.js
module.exports = { schema: true, attributes: { //add referecnce other article articles: { collection: 'article', via:'ref_category_id' }, category_name: { type:'string', required: true, unique: true }, } };
article.js
module.exports = { schema: true, attributes: { //adding reference category ref_category_id: { collection:'category', via:'articles' }, //adding reference city ref_city_id: { model:'city' }, //add reference area ref_area_id: { model:'areas' }, //adding reference tags tags: { collection: 'tag', via:'articles' }, title:{ type:'string',required:true}, blurb: { type: 'string'}, description:{ type:'string'} } };
city.js
module.exports = { schema: true, attributes: { areas: { collection: 'areas', via:'ref_city_id' }, ref_article_id:{ model:'article' }, city_name: { type:'string', required:true, unique:true } } };
areas.js
module.exports = { schema: true, attributes: { area_name: { type:'string',required:true}, latitude: { type:'float'}, longitude: { type:'float'}, ref_city_id: { model: 'city' }, ref_article_id:{ model:'article' } } };
tag.js
module.exports = { schema:false, attributes: { //adding reference article articles: { collection: 'article', via:'tags' }, tag_name:{type:'string', required:true, unique:true } } };
categorycontroller.js
searchcategory: function(req, res, next) { var category_name = req.param('category_name'); category.find({category_name:{'like': '%'+category_name+'%'}}).populate('articles').exec(function(err, category) { if(err) { return res.json({'status':486,'status_message':'server error'}); } else { if(category.length > 0) { var c = parseint(category[0].articles.length,10); console.log(c); var = parseint('0',10); (i=0; i<c; i++) { console.log('i value in loop = ' + i); article.find({id:category[0].article[i].id}).populateall().exec(function(err,article_info) { if(err) { return res.send(err); } else { console.log(article_info); console.log('-------------------------------------------'); res.json(article_info); console.log(' value = ' + i); } }); } //console.log(category); //return res.json({'status':479,'status_message':'success..!!','category_info':category}); } else { return res.json({'status':489,'status_message':'failure..!! no categories found..!!'}); } } }); }
postman request:
http://localhost:1337/category/searchcategory {"category_name":"travel"}
this json response: here getting article mapped category. wanted display values of city, areas , tags mapped article.
{ "status": 479, "status_message": "success..!!", "category_info": [ { "articles": [ { "ref_city_id": "55a766d0a29811e875cb96a1", "ref_area_id": "55a78b69578393e0049dec43", "title": "title", "blurb": "blurb", "description": "description", "createdat": "2015-07-16t12:36:36.778z", "updatedat": "2015-07-16t12:48:20.609z", "id": "55a7a55439ace79e0512269d" }, ], "category_name": "cooking ", "id": "55a6b26aee9b41de747547bb", "createdat": "2015-07-15t19:20:10.670z", "updatedat": "2015-07-15t19:20:10.670z" } ] }
how deep nested associations using populate? or other way achieve this?
please can me achieve this.
thanks in advance.
yes, there way this.
make sure require following npm module.
var nestedpop = require('nested-pop');
searchcategory: function(req, res, next) { var category_name = req.param('category_name'); category.find({category_name:{'like': '%'+category_name+'%'}}) .populate('articles') .exec(function(err, category) { if(err) return res.json({'status':486,'status_message':'server error'}); if(category.length > 0) { var c = parseint(category[0].articles.length,10); console.log(c); var = parseint('0',10); (var = 0; < c; i++) { console.log('i value in loop = ' + i); article.find({id:category[0].article[i].id}) .populateall() .exec(function(err, article_info) { if(err) return res.send(err); return nestedpop(article_info, { ref_city_id: { as: 'city', populate: [ 'things', 'you', 'want', 'to', 'populate', 'for', 'city' ], }, ref_area_id: { as: 'area', // or areas (whatever model name is) populate: [ 'things', 'you', 'want', 'to', 'populate', 'for', 'area' ] } }).then(function(article_info) { console.log(article_info); console.log('------------------------------------------'); res.json(article_info); console.log(' value = ' + i); }); }); } } else { return res.json({'status':489,'status_message':'failure..!! no categories found..!!'}); } }); }
go https://www.npmjs.com/package/nested-pop more documentation.
Comments
Post a Comment