1

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 ?

1 Answer 1

2

yes you can achieve this with help of Async-Await

exports.allProperties = async function (req, res) {
    let categoryList = [];
    let propertyList = [];
    let usersIds = [];
    let categories = await Category.find({});
    categories.forEach(function (category) {
        categoryList[category.id] = category;
    });

    let properties = await Property.find({});
    properties.forEach(function (property) {
        propertyList.push(property);
        usersIds.push(property.user_id)
    });


    let users = await User.find({ 'id': { $in: usersIds } });
    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
    }); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.