K
K
Kotaro Hiba2021-08-25 12:37:50
typescript
Kotaro Hiba, 2021-08-25 12:37:50

How to pass data from middleware to next method?

Hello everyone, mb stupid question, but how to pass data from middlevare to the next method?
This is what the controller looks like

UserController.get(
  "",
  AuthorizeMiddleware,
  RolesMiddleware(["ADMIN", "USER"]),
  UserService.getUsers
)

In the AuthorizeMiddleware, I decrypt the jwt token and pass the data through the assignment to req.body._user
At first everything was ok, but after that I ran into some problems because I constantly have to remove _user from the object in the code.

I tried to save the data in a separate req._user key,
but TS gives an error TS2339: Property '_user' does not exist on type 'Request >', which is logical in principle, I thought, okay, I'll make a new interface that inherits Requset and just add a field there _user, everything was ok at first, but then other errors started.
Tell me how to correctly transfer data from the middleware to the next requests, with the backend and express while on you, only the 1st day I share the information, not everything is clear yet, I think that you need to designate an additional field for TS somehow,

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dispache, 2021-08-25
@lina666

app.locals ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question