0

I've got this code:

var user;
app.use(
  "/projects",
  (req, res, next) => {
    user = req.user;
    next();
  },
  new DashboardService(user)
);

But, since user is being re-defined in the callback, it's value isn't saved, thus when it creates a DashboardService, user returns null. How can user not return null when it's re-defined in the callback?

Here's an example of what I mean https://codepen.io/lachie-source/pen/RwaQNgq

4
  • Yes, but then it doesn't stay after the callback. So how can I make it save? Commented Sep 7, 2020 at 21:27
  • What are you trying to achieve? What is the purpose of the DashboardService? Do you want to create an instance of that object for every request? Commented Sep 7, 2020 at 21:27
  • You pass new DashboardService(user) as the third paramter to app.use, but app.use only expects two parameters.... Commented Sep 7, 2020 at 21:30
  • From how I understand your questions, it seems you have not really understood how server side web applications work, especially that they can handle multiple requests by different users at the same time. Try to go through some tutorials to get a better understanding of how this works. Commented Sep 7, 2020 at 21:41

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

exactly, what about wrapping it in a function? (req,res,next) => {new DashboardService(req.user); next();}
@sonkatamas It's not clear to me what the DashboardService thing is good for. You are speculating that the OP wants a separate one for each request ;)
Is there a way the callback can happen before creating the constructor?
@BumpyDevelopment No, the callback is there to handle http requests by users. You can't control when users make requests. Also, the application can handle multiple requests by different users at the same time. So, it makes no sense to have one global "user" information. There can be multiple users at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.