Skip to content

Instantly share code, notes, and snippets.

@andrepadez
Created January 29, 2013 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrepadez/4663588 to your computer and use it in GitHub Desktop.
Save andrepadez/4663588 to your computer and use it in GitHub Desktop.
Location = {
referencia: {
type: Number,
unique: true
},
owner: {
type: Schema.Types.ObjectId,
ref: 'Owner'
},
accounts: [
{
type: Schema.Types.ObjectId,
ref: 'Account'
}
],
...
};
Account = {
nome: {
type: String
},
telemovel: {
type: String
},
contacto2: {
type: String
},
email: {
type: String
},
nif: {
type: Number
}
};
//Accounts Controller
var list = function(req, res){
Account.find().sort({nome: 1}).exec(function(err, accounts){
//what is the best way to find out what locations each owner has?
//right now, i am doing:
var data = {};
data.accounts = accounts;
if(accounts.length === 0){
res.render('accounts', data);
return;
}
for(var i = 0; i < accounts.length; i++){
(function(idx){
var account = accounts[idx];
Location.find({accounts: account._id}, function(err, locations){console.log('locations found');
account.locations = locations;
console.log(idx, account);
if(idx + 1 === accounts.length){
res.render('accounts', data);
}
});
})(i);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment