Reputation: 8292
I have 3 models (Room, Module and Device) : Room :
/**
* Room.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
attributes : {
name : {
type : 'string',
required : true
},
image : {
type : 'binary'
},
modules : {
collection : 'Module',
via : 'inRoom'
},
toJSON : function ()
{
var obj = this.toObject();
if (obj.image)
{
var base64data = new Buffer(obj.image.toString(), 'binary').toString();
obj.image = base64data;
}
return obj;
}
},
beforeCreate : function (attrs, next)
{
next();
}
,
beforeUpdate : function (attrs, next)
{
next();
}
}
;
Module :
/**
* Module.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
attributes : {
name : {
type : 'string',
required : true
},
nbDevices : {
type : 'integer',
defaultsTo : 1
},
image : {
type : 'binary'
},
inRoom : {
model : 'Room'
},
devices : {
collection : 'Device',
via : 'module'
}
}
};
Device :
/**
* Device.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
attributes : {
name : {
type : 'string',
required : true
},
index : {
type : 'integer',
defaultsTo : 0
},
value : {
type : 'integer',
defaultsTo : 0
},
image : {
type : 'binary'
},
module : {
model : 'Module'
}
}
};
I want to retreive all my Rooms with all Devices in it. For now I do like this :
Room.find().populate('modules')
.exec(function (err, rooms)
{
var index = 0;
var total = rooms.length-1;
_(rooms).forEach(function (room)
{
Device.find({module : _.pluck(room.modules, 'id')}).populate("module").exec(function (err, data)
{
room.devices = data;
console.log(room);
if(total == index)
{
return res.json(rooms);
}
index++;
});
}).value();
});
But it doesn't look clean/safe method. Is there another way a achieve this ? I see this post Sails.js populate nested associations but can't make it to work with a find instead of findOne.
Upvotes: 0
Views: 367
Reputation: 1762
You can use async or some other library for control flow to add to the example you mentioned in your answer to do something like this:
var async = require('async');
Room.find()
.populate('modules')
.exec(function (err, rooms) {
// if(err) ...
async.forEach(room,
// apply on each room
function(room, cb){
if(err || !room) return cb(err);
Device.find({module : _.pluck(room.modules, 'id')})
.populate("module")
.exec(function(err, devices){
room.devices = devices;
cb();
});
},
// when all rooms are done
function(err){
// if(err) ...
res.json(rooms);
}
);
});
Upvotes: 1