In my NodeJS application, I am building an API that first fetches all tarrifs name from tarrifs collection then based on all those tarrifs I want to return the counts of these tariffs allocated to users I tried the below-given code
router.get('/getTarrifDetails', (req,res,next) => { result=[]; tname=[]; counts=[];Tarrif.find().distinct('tarrif_type', (err,docs) => { docs.forEach((ele) => { tname.push(ele); User.countDocuments({tarrif_type:ele}, (uerr,usr) => { counts.push(usr); }); result.push(ele); }); result.push(counts);
});
});
When I console.log(result)
it only shows one array of tarrif_type
and other array is empty
#node-js