-
-
Save rvagg/4663748 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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