The code in the callback is not executed when app.use runs. It is only executed later on (and multiple times) whenever there is a request to your application with the given route. So, the user variable is only assigned when there is a request, and newly assigned for each new request.
The call to the DashboardService constructor happens right away. The user variable doesn't have a value at that point in time yet.
If you want to create a separate instance of DashboardService for each request, you have to do that within the callback function:
app.use(
"/projects",
(req, res, next) => {
let user = req.user;
new DashboardService(user); // But why create an object that isn't used
next();
}
);
If you want to have a global status which single user is currently interacting with your application: That's not possible because your application can handle multiple requests by different users at the same time.
DashboardService? Do you want to create an instance of that object for every request?new DashboardService(user)as the third paramter to app.use, but app.use only expects two parameters....