diff --git a/routes/admin.js b/routes/admin.js index c18e403..7860b59 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -151,10 +151,7 @@ router.get('/admin/dashboard', restrict, async (req, res) => { _id: '$o.v.productId', title: { $last: '$o.v.title' }, productImage: { $last: '$o.v.productImage' }, - count: - { - $sum: '$o.v.quantity' - } + count: { $sum: '$o.v.quantity' } } }, { $sort: { count: -1 } }, { $limit: 5 } @@ -162,8 +159,14 @@ router.get('/admin/dashboard', restrict, async (req, res) => { }; // Fix aggregate data - dashboardData.ordersAmount = dashboardData.ordersAmount[0].sum; - dashboardData.productsSold = dashboardData.productsSold[0].sum; + if(dashboardData.ordersAmount.length > 0){ + dashboardData.ordersAmount = dashboardData.ordersAmount[0].sum; + } + if(dashboardData.productsSold.length > 0){ + dashboardData.productsSold = dashboardData.productsSold[0].sum; + }else{ + dashboardData.productsSold = 0; + } res.render('dashboard', { title: 'Cart dashboard', diff --git a/views/dashboard.hbs b/views/dashboard.hbs index fc3978d..b8de9f2 100644 --- a/views/dashboard.hbs +++ b/views/dashboard.hbs @@ -53,6 +53,7 @@
Top products sold
{{/each}} + {{else}} +
Nothing to see yet. Do some orders.
+ {{/if}}