Я использую node.js с библиотекой asyn (https://github.com/caolan/async). Когда я использую функцию async.each его рабочую, но это изменение элемента массива заказов, так что я не могу перечислить категории с некоторой сортировкой ..node.js - порядок сортировки элементов массива async.each автоматически изменяется
module.exports = {
index: function (req, res) {
var async = require('async');
var data = new Object();
data.title = "";
data.meta_keywords = "";
data.meta_description = "";
data.category = new Array();
bredcrumbs = new Array();
bredcrumbs[0] = {'text':'Home','link':'Link','active':false};
category_list = new Array();
categories = new Array();
async.waterfall([
function(callback){
Category.find({sort: 'name ASC'}).done(function(err,cat_data){
//console.log(cat_data);
categories = cat_data;
callback(null);
});
},
function(callback){
async.each(categories, SaveData, function(err){
// if any of the saves produced an error, err would equal that error
callback(null);
});
},
], function() {
data.category = category_list;
//console.log(category_list);
res.view('pages/home',data);
});
function SaveData(item,callback){
Word.count({category_id:item['id'],status:'1',is_approved:'1'}).done(function(err, total){
t_category = new Array();
t_category['name'] = item['name'];
t_category['keyword'] = item['keyword'];
t_category['link'] = "http://"+req.headers.host+"/game/"+item['keyword'];
t_category['total'] = total;
category_list.push(t_category);
callback(null);
});
}
},
/**
* Overrides for the settings in `config/controllers.js`
* (specific to HomeController)
*/
_config: {}
};
Я использую функцию async.each
async.each(categories, SaveData, function(err){
// if any of the saves produced an error, err would equal that error
callback(null);
});
async.eachSeries() - лучшее решение ... – balaphp