Post.find({}, function (error, data){
var projects = [];
for (var index = 0; index < data.length; index++) {
projects.push({
image: "none",
name: "none",
profilePicture: "none",
profession: "none"
});
}
// reverse post order
function asyncLoop(i, callback) {
if (i>=0){
projects[data.length-i-1].image = data[i].imagelink[0];
User.find({'_id' : data[i].author}, function(error, userdata){
projects[data.length-i-1].name = userdata.local.name+ " " + userdata.local.surname;
});
asyncLoop(i-1, callback);
} else { callback(); }
}
asyncLoop(data.length-1, function() {
console.log('callback');
});
The error occurs here:
projects[data.length-i-1 ].name = userdata.local.name+ " " + username.local.surname;
I suspect the issue lies in the assignment inside a Find query but I'm not sure how to resolve it.