【问题标题】:How to use async in Nodejs如何在 Nodejs 中使用异步
【发布时间】:2019-01-21 04:38:00
【问题描述】:

我在一个问题上是结构,但无法找到相同的解决方案,我正在研究 nodejs express 框架,下面我已经放置了我现有的代码,我是结构,就像我可以异步调用函数一样。

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
        });
    }); 
}

在这里,我想获取所有属性,但属性具有类别和用户,因此在属性调用内部我必须调用 getProperties(),并且从 getProperties 中相同,我必须调用 getUsers,可能像下面这样我想实现。

var 属性 = Property.find({});

var categories = Category.find({});

var users = User.find({});

所以我可以在这三个之后的单行代码中的所有属性、类别和用户,我将管理代码并生成正确的响应并返回或呈现视图。

我怎样才能做到这一点?

【问题讨论】:

    标签: node.js mongodb express asynchronous


    【解决方案1】:

    是的,你可以在 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
        }); 
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-15
      • 1970-01-01
      • 2020-07-11
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2014-03-28
      相关资源
      最近更新 更多