Created
January 29, 2013 11:30
-
-
Save andrepadez/4663588 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; | |
} | |
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