Skip to content

Instantly share code, notes, and snippets.

@rvagg
Forked from andrepadez/gist:4663588
Last active December 11, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvagg/4663748 to your computer and use it in GitHub Desktop.
Save rvagg/4663748 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;
}
var completed = 0
accounts.forEach(function (account) {
Location.find({accounts: account._id}, function(err, locations){
console.log('locations found');
account.locations = locations;
console.log(idx, account);
if(++completed === accounts.length){
res.render('accounts', data);
}
});
})
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment