currently I am on the middle of a ecommerce project using node. So, the problem is i want to show the count of notifications that admin received on the admin side. The notifications are the orders that user’s requested to return. because of that o want to show the notification coount on all admin ppages. To solve it i made a middleware.all to run whenever admin changes a page.
so, when the page loads I given a middleware on admin route page as following :-
admin_route.all(async (req, res, next) => { // console.log('Middleware executed'); try { const adminNotificationsCount = await Notifications.find({ matter: 'returnOrderRequest' }).countDocuments(); res.locals.adminNotifications = adminNotificationsCount; next(); } catch (error) { console.log(error.message); }; });
But the middleware is not working, because of that there has nothing on the global variable.
is there any problem with this or any other method to implement?
Afsal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.