I am struct at one problem and not able to find the solution for the same, I am working on nodejs express framework, below i have put my existing code, i am struct like how i can call the functions async.
exports.allProperties = function(req, res){
var categoryList = [];
Category.find({}, function(err, categories) {
categories.forEach(function(category) {
categoryList[category.id]=category;
});
getProperties(categoryList,req,res);
});
}
function getProperties(categoryList,req,res){
var propertyList = [];
var usersIds = [];
Property.find({},function(err, properties){
properties.forEach(function(property) {
propertyList.push(property);
usersIds.push(property.user_id)
});
getUsers(categoryList,propertyList, usersIds,req,res);
});
}
function getUsers(categoryList,propertyList, usersIds,req,res){
var usersList = [];
User.find({'id':{ $in: usersIds }},function(err,users){
users.forEach(function(user) {
usersList[user.id] = [];
usersList[user.id]["first_name"] = user.first_name;
usersList[user.id]["last_name"] = user.last_name;
usersList[user.id]["mail"] = user.last_name;
usersList[user.id]["contact_number"] = user.contact_number;
});
res.render('admin/allProperties.ejs', {
error : req.flash("error"),
success: req.flash("success"),
categories: categoryList,
userLists: usersList,
properties: propertyList
});
});
}
Here i want to get all properties but property has category and Users so inside of properties call i have to call getProperties() and same from getProperties i have to call getUsers it is possible like below which i would like to achive.
var properties = Property.find({});
var categories = Category.find({});
var users = User.find({});
So i can all properties, categories and users in single line of code after this three i will managed code and generate proper response and return or render the view.
How i can achieve this ?