Skip to content

Injecting request/response into services, repositories or other container managed entities. #8107

Answered by achrinza
roberttaylortech asked this question in Q&A
Discussion options

You must be logged in to vote

It seems that this is happening at startup. It works on Controllers as they are instantiated on a per-request basis, meaning that the binding is already populated by the time the Controller instance is created. In contrast, Services, Components, etc. are instantiated on startup by default. This means that no HTTP request is active at the time the binding is being resolved, hence why it's throwing that error.

A quick fix would be to use Getters:

// Imports
import {Getter, inject} from '@loopback/rest';
import {Request, RestBindings} from '@loopback/rest';
...
// Constructor / Property / Method dependency injection
@inject.getter(RestBindings.Http.REQUEST) protected reqGetter: Getter<Request>;

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by achrinza
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants