2👍
✅
You should run all those queries in parallel using Promise.all
and when all of them done render chart
const userPromise = db.User.findOne({
where: {
username: req.session.user
}
});
const tasksWithCountPromise = db.Tasks.findAndCountAll({
attributes: ['category'],
where: {
UserUsername: req.session.user
},
group: 'category'
}).then(function (result) {
for (var i = 0; i < result.rows.length; i++) {
categoryList.push(result.rows[i].dataValues.category);
countList.push(result.count[i].count);
}
return { countList, categoryList };
});
const allTasksPromise = db.Tasks.findAll({
where: {
UserUsername: req.params.username
}
});
Promise.all(userPromise, tasksWithCountPromise, allTasksPromise)
.then(([user, tasksWithCount, allTasks]) => {
res.render('index', {
data: allTasks,
helpers: {
photo: user.dataValues.picURL,
countList: tasksWithCount.countList,
categoryList: tasksWithCount.categoryList
}
})
});
Source:stackexchange.com