[英]How to use async in Nodejs
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. 我是一个问题的结构,无法找到相同的解决方案,我正在使用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
});
});
}
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. 在这里,我想获取所有属性,但属性具有类别和用户,因此在属性调用内,我必须调用getProperties(),而在getProperties中,我必须调用getUsers,有可能像下面这样,我想实现。
var properties = Property.find({}); var properties = Property.find({});
var categories = Category.find({}); var Categories = Category.find({});
var users = User.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 ? 我怎样才能做到这一点?
yes you can achieve this with help of Async-Await 是的,您可以在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
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.