My backend code looks like:
exports.getDashboard = (req, res) => {
const dashboard = {};
const query = {};
let start;
let end;
async.parallel([
(callback) => {
const { role } = req.user;
switch (role) {
case 'Admin':
console.log('case admin is working >> >> >> >> >> >> >> >> >> >> ');
start = moment().startOf('day'); // set to 12:00 am today
end = moment().endOf('day'); // set to 23:59 pm today
query.where = {
entryTime: {
$gt: start,
$lt: end,
},
};
break;
case 'Manager':
console.log('case manager is working >> >> >> >> >> >>> >> >> >> >> >> >> >>');
Staff.findOne({
where: {
loginId: req.user.id,
},
}).then((staffData) => {
console.log('staffData', JSON.stringify(staffData));
query.where = {
entryTime: {
$gt: start,
$lt: end,
},
branchId: staffData.branchId,
};
});
break;
default:
console.log('default case');
}
console.log('query >> >> >> >> >> >> >> >> >> >> >> >> >> >> >>', query);
// query is undefined if my case is manager async call.
Visitor.findAndCountAll(query)
.then((data) => {
dashboard.todayentries = data;
callback();
});
},
], (err) => {
if (err) return res.status(500).send(err);
return res.status(200).send(dashboard);
});
};
If my case is Manager query become undefined.
I already ask one question for async call, they recommended to use callbacks, its okay, but how to use nested callback inside async.parallel method?
NOTE: I dont want to erase my async.parallel method.
I tried with another callback, Working fine.
Here what i tried:
Is this good approach to use?