I have a chain of promises in nodejs. They look like this:
codecommon.db.executeDbCommandAsync({
'aggregate':'table1',
pipeline:[
{$match:{uid:uid}},
{$match :{timestamp:{$gt:start_week,$lt:end_week}}},
{$group:{_id:{"pageSession":"$pageSession"}, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
{$match:{totalpersession:{$gt:1}}},
{$project:{seconds:{$subtract:["$max","$min"]}}}
]
}) .then(
function(result) {
dayspv = get_days(result[0].documents[0].result);
totaldays = sum_days(dayspv,totaldays); //vb interaction
return common.db.executeDbCommandAsync({
'aggregate':'table2',
pipeline:[
{$match:{uid:uid}},
{$match :{timestamp:{$gt:start_week,$lt:end_week}}},
{$group:{_id:{"pageSession":"$pageSession"}, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
{$match:{totalpersession:{$gt:1}}},
{$project:{seconds:{$subtract:["$max","$min"]}}}
]
}); }) . then( function(result) {
days = get_days(result[0].documents[0].result);
totaldays = sum_days(days,totaldays);
//sm interaction return common.db.executeDbCommandAsync({
'aggregate':'table3',
pipeline:[
{$match:{uid:uid}},
{$match :{timestamp:{$gt:start_week,$lt:end_week}}},
{$group:{_id:{"session":"$session"}, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
{$match:{totalpersession:{$gt:1}}},
{$project:{seconds:{$subtract:["$max","$min"]}}}
]
});
}) .then( ...etc
Taking into account that in every promise i have same mongo query. I want to make a function like this:
var check_interaction = function(table, uid, start_week, end_week){
return common.db.executeDbCommandAsync({
'aggregate':'\'' + table + '\'',
pipeline:[
{$match:{uid:uid}},
{$match :{timestamp:{$gt:start_week,$lt:end_week}}},
{$group:{_id:{"session" :"$session" }, totalpersession:{$sum:1}, min:{$min:"$timestamp"}, max:{$max:"$timestamp"}}},
{$match:{totalpersession:{$gt:1}}},
{$project:{seconds:{$subtract:["$max","$min"]}}}
]
});
};
and switch the promise chain to something like that:
check_interaction(...) .then( process some data
return another check_interaction(...);)
.then( process some data
return another check_interaction(...);)
.then(...etc
All mongoskin functions/queries are promisified, it dosen't throw an error, goes through whole chain(debugged with console logs), thou no data provided from one to another. Can someone help me?