When I run the below for loop, month increment every for steps and give in getUserNumberAndPaymentsbyMonth
function as a below:
EDIT
for(var i = 1; i<= getCurrentMonth; i ){
console.log("getting month ", i)
var userNum = await getUserNumberAndPaymentsbyMonth(companyName, i).then((data)=>{
getUserCountperMount.push(data[1])
}
).catch(console.error);
}
But sometimes getUserNumberAndPaymentsbyMonth returns "0" so I debug this function. And this function as in below:
EDIT:
async function getUserNumberAndPaymentsbyMonth(operatorName, month) {
// const operator = await Operators.findOne({operatorName});
// var myDate = new Date(2022, 7, 19);
console.log(month);
var totalPaymentsPerMouth = 0;
var totalUserNumberPerMount = 0;
var value = await Operators.aggregate(
[
{ $unwind: "$users" },
{ $match: { operatorName: operatorName } },
{
$project: {
month: { $month: "$users.paymentsData" },
operatorName: 1,
users: "$users"
}
},
{
$match: {
// 'users.paymentsData': { "$gte": new Date("2022-07-24"), "$lt": new Date("2022-07-27")},
month: {
$eq: month
}
}
}
],
function (err, docs) {
if (err) {
console.log("db getting errror");
console.log(err);
// return [0,0];
} else {
docs.forEach(function (data) {
totalUserNumberPerMount = totalUserNumberPerMount 1;
totalPaymentsPerMouth = totalPaymentsPerMouth data.users.payment;
});
console.log("getIn");
console.log(totalPaymentsPerMouth);
console.log(totalUserNumberPerMount);
}
}
);
console.log("output: ", totalPaymentsPerMouth, totalUserNumberPerMount);
return [totalPaymentsPerMouth, totalUserNumberPerMount];
}
When I debug this function, when I get the false value, after a bit time later I get correct value below getIn
console output. but it's too late for me to get this value because it's out of the for loop. How cna I write the code properly. Where is my fault.
I want that a value is not returned in the getUserNumberAndPaymentsbyMonth function until the aggregate query is completed. If there is an error in the query, I can catch it in the error section, but this query should not exit before the end of the function.
I get this output:
getting month 1
1
getIn
0
0
output: 0 0
getting month 2
2
getIn
0
0
output: 0 0
getting month 3
3
output: 0 0
getting month 4
4
getIn
0
0
getIn
0
0
output: 0 0
getting month 5
5
getIn
0
0
output: 0 0
getting month 6
6
getIn
0
0
output: 0 0
getting month 7
7
output: 0 0
In function last: [
0, 0, 0, 0,
0, 0, 0
]
[
0, 0, 0, 0,
0, 0, 0
]
**getIn. // correct value return after loop, getIn log working after a while.
1395
136**
CodePudding user response:
As I can see you are providing a callback to your aggregate function which would execute for the result of aggregate query.
But as you need the output to be returned by your getUserNumberAndPaymentsbyMonth() function you don't need to put callback function there just rewrite your function like this
async function getUserNumberAndPaymentsbyMonth(operatorName, month) {
// const operator = await Operators.findOne({operatorName});
// var myDate = new Date(2022, 7, 19);
console.log(month);
var totalPaymentsPerMouth = 0;
var totalUserNumberPerMount = 0;
var docs = await Operators.aggregate(
[
{ $unwind: "$users" },
{ $match: { operatorName: operatorName } },
{
$project: {
month: { $month: "$users.paymentsData" },
operatorName: 1,
users: "$users"
}
},
{
$match: {
// 'users.paymentsData': { "$gte": new Date("2022-07-24"), "$lt": new Date("2022-07-27")},
month: {
$eq: month
}
}
}
]
).toArray();
//check for valid errors
if (!docs) {
console.log("db getting errror");
console.log(err);
// return [0,0];
} else {
docs.forEach(function (data) {
totalUserNumberPerMount = totalUserNumberPerMount 1;
totalPaymentsPerMouth = totalPaymentsPerMouth data.users.payment;
});
console.log("getIn");
console.log(totalPaymentsPerMouth);
console.log(totalUserNumberPerMount);
return [totalPaymentsPerMouth, totalUserNumberPerMount];
}
}