异步控制器 sails.js
asynchronous controllers sails.js
变量 obj 仍然清空到 console.log ( obj) 中,我如何完成 ahcer 搜索并打印包含所有数据的变量?
'showservices': function (req, res, next) {
Service.find(...., function (err, services) {
if (err) return next(err);
var obj = [];
_.each(services, function(s){
SaleDetail.find({id_service:s.id_service}, function (err, details){
var total = 0
var cont = 0
_.each(details, function(d){
total = total + parseFloat(d.fullPrice);
cont ++;
});
obj.push({
name: s.serviceName,
cant: cont,
total: total,
});
console.log(obj)
});
});
console.log(obj)
});
},
你代码中的一些东西,我做了一个jsbin(当然不是在jsbin上工作),其中一段代码可能会帮助你解决你的问题,仔细阅读我添加的评论。
http://jsbin.com/howanojoka/1/edit?js
我做了几个中间输出,如果这不能解决您的问题,请控制台记录修改后代码的输出以适应您的问题。
这里是为那些不想访问 jsbin 的人准备的代码副本:
'showservices': function (req, res, next) {
Service.find('', function (err, services) {
if (err) return next(err);
//we are in sails so lets log properly
sails.log.info(services.length); //if 0 your problem may be here...
var serLen=services.length ; //storing the value of length for checking completin (faster than calling each time services.length ;)
var obj = [];
var completeService=0;
_.each(services, function(s){
SaleDetail.find({id_service:s.id_service}, function (err, details){
//are you sure you have no error here ....
if(err) return next(err); //why not here ?
//again are you sure you have a result
sails.log.info(details.length);//if 0 your problem may be here as well
var total = 0
var cont = 0
_.each(details, function(d){
total = total + parseFloat(d.fullPrice); //you could write total+=parseFLoat(d.fullPrice); just an info :)
cont ++;
});
obj.push({
name: s.serviceName,
cant: cont,
total: total,
});
sails.log.info(obj)//let's use sails log again :)
completeService++;
if(completeService===serLen){
sails.log.info(obj)//here is your completed object
return next();
}
});
});
//your global problem i assume is when to "return" as you have async ? so i gave a try look abovee:)
sails.log.info(obj)//this will be executed before any or some SaleDetail.find() as as your SaleDetail.find is async, in clear empty array
});
},
请使用async
'showservices': function (req, res, next) {
async.auto({
services: function(callback){
Service.find(....).exec(callback);
},
result: ['services', function(callback,results){
var obj = [];
async.each(results.services, function(s, innercb){
SaleDetail.find({id_service:s.id_service}).exec(function(err, details){
var total = 0
var cont = 0
_.each(details, function(d){
total = total + parseFloat(d.fullPrice);
cont ++;
});
obj.push({
name: s.serviceName,
cant: cont,
total: total,
});
innercb();
});
}, function(err){
callback(err, obj);
});
}],
}, function(err,result){
if (err) return next(err);
console.log(result.result);
});
},
变量 obj 仍然清空到 console.log ( obj) 中,我如何完成 ahcer 搜索并打印包含所有数据的变量?
'showservices': function (req, res, next) {
Service.find(...., function (err, services) {
if (err) return next(err);
var obj = [];
_.each(services, function(s){
SaleDetail.find({id_service:s.id_service}, function (err, details){
var total = 0
var cont = 0
_.each(details, function(d){
total = total + parseFloat(d.fullPrice);
cont ++;
});
obj.push({
name: s.serviceName,
cant: cont,
total: total,
});
console.log(obj)
});
});
console.log(obj)
});
},
你代码中的一些东西,我做了一个jsbin(当然不是在jsbin上工作),其中一段代码可能会帮助你解决你的问题,仔细阅读我添加的评论。
http://jsbin.com/howanojoka/1/edit?js
我做了几个中间输出,如果这不能解决您的问题,请控制台记录修改后代码的输出以适应您的问题。
这里是为那些不想访问 jsbin 的人准备的代码副本:
'showservices': function (req, res, next) {
Service.find('', function (err, services) {
if (err) return next(err);
//we are in sails so lets log properly
sails.log.info(services.length); //if 0 your problem may be here...
var serLen=services.length ; //storing the value of length for checking completin (faster than calling each time services.length ;)
var obj = [];
var completeService=0;
_.each(services, function(s){
SaleDetail.find({id_service:s.id_service}, function (err, details){
//are you sure you have no error here ....
if(err) return next(err); //why not here ?
//again are you sure you have a result
sails.log.info(details.length);//if 0 your problem may be here as well
var total = 0
var cont = 0
_.each(details, function(d){
total = total + parseFloat(d.fullPrice); //you could write total+=parseFLoat(d.fullPrice); just an info :)
cont ++;
});
obj.push({
name: s.serviceName,
cant: cont,
total: total,
});
sails.log.info(obj)//let's use sails log again :)
completeService++;
if(completeService===serLen){
sails.log.info(obj)//here is your completed object
return next();
}
});
});
//your global problem i assume is when to "return" as you have async ? so i gave a try look abovee:)
sails.log.info(obj)//this will be executed before any or some SaleDetail.find() as as your SaleDetail.find is async, in clear empty array
});
},
请使用async
'showservices': function (req, res, next) {
async.auto({
services: function(callback){
Service.find(....).exec(callback);
},
result: ['services', function(callback,results){
var obj = [];
async.each(results.services, function(s, innercb){
SaleDetail.find({id_service:s.id_service}).exec(function(err, details){
var total = 0
var cont = 0
_.each(details, function(d){
total = total + parseFloat(d.fullPrice);
cont ++;
});
obj.push({
name: s.serviceName,
cant: cont,
total: total,
});
innercb();
});
}, function(err){
callback(err, obj);
});
}],
}, function(err,result){
if (err) return next(err);
console.log(result.result);
});
},