loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Error "XXX is not bound to any value in context YYY" after latest update (@loopback/[email protected])

Open dkrantsberg opened this issue 7 years ago • 7 comments

The key 'xxx' is not bound to any value in context yyyy error after latest update (@loopback/[email protected]).

After updating to the latest loopback packages from (@loopback/[email protected] and others) our API application started to fail because repository can't resolve injected dependency.

I bound my dependency upon every request inside sequence.ts

sequence.ts

      if (route.pathParams.userId) {
        const user = await this.userRepo.findById(route.pathParams.userId);
        context.bind(MyBindings.CURRENT_USER).to(user);
     }

Then I inject it in the repository:

User list repository

export class ListRepository extends DefaultCrudRepository<List, typeof List.prototype.id> {
  constructor(
    @inject('datasources.MyAppDb') myAppDb: MyAppDataSource,
    @inject(MyBindings.CURRENT_USER) private user: User
  ) {
    super(List, myAppDb);
  }

Current Behavior

Error: The key 'current.user' is not bound to any value in context 2d8dcd10-4de8-11e9-9149-b1a24c04504a

Expected Behavior

MyBindings.CURRENT_USER should resolve to the value bound in sequence.ts

Upon debugging I was able to find out that in the latest version of Loopback it's trying to get value of MyBindings.CURRENT_USER from the application (MyApp) context instead of RequestContext.

https://github.com/strongloop/loopback-next/blob/master/packages/context/src/context.ts#L725

dkrantsberg avatar Mar 24 '19 04:03 dkrantsberg

Do you have ListRepository bound in SINGLETON scope? See https://github.com/strongloop/loopback-next/blob/master/docs/site/Dependency-injection.md#dependency-injection-for-bindings-with-different-scopes

raymondfeng avatar Mar 24 '19 04:03 raymondfeng

@raymondfeng I'm not binding ListRepository explicitly. I guess Loopback is binding it behind the scenes.
This repository is being injected as a dependency in the ListController.

export class ListController {
  constructor(
    @repository(ListRepository) public listRepository: ListRepository
  ) {}
......  
}

dkrantsberg avatar Mar 24 '19 21:03 dkrantsberg

In MyApp.registry FacilityRepository is bound in TRANSIENT scope. Just like all other repositories and controllers.

image

The question is why when resolving MyBindings.CURRENT_USER binding Loopback is looking in the application context instead of RequestContext where this binding actually exists? It doesn't even attempt to look in RequestContext first.

This only started to happen since the latest LB release on March 22.

dkrantsberg avatar Mar 24 '19 22:03 dkrantsberg

Do you have a simple repo to reproduce the issue? If so, I can investigate.

raymondfeng avatar Mar 24 '19 22:03 raymondfeng

@raymondfeng, I've created a stripped-down version of our project which reproduces this error: https://github.com/dkrantsberg/lb-my-api

To reproduce:

npm i
npm start
do GET http://127.0.0.1:8000/api/facilities/facility1/lists

If you put a conditional breakpoint here in context.ts (condition: key === 'current.facility') you'll see that this is the application instance, not RequestContext.

I was able to trace it to the fact that ListRepository has dependency on FacilityInstanceDatasource https://github.com/dkrantsberg/lb-my-api/blob/master/src/repositories/list.repository.ts#L11 which in its turn has also dependency on current Facility: https://github.com/dkrantsberg/lb-my-api/blob/master/src/datasources/facility-instance.datasource.ts

This is done because in our project we have a need to dynamically change/configure FacilityInstanceDatasource datasource at run-time based specific facility which comes from HTTP request.

dkrantsberg avatar Mar 25 '19 16:03 dkrantsberg

@dkrantsberg Please note DataSource by default is bound as singletons. To fix your problem, you need to add the following to https://github.com/dkrantsberg/lb-my-api/blob/master/src/datasources/facility-instance.datasource.ts#L6.

@bind({scope: BindingScope.TRANSIENT})
export class FacilityInstanceDatasource extends juggler.DataSource {
}

raymondfeng avatar Mar 25 '19 18:03 raymondfeng

@raymondfeng This makes sense. I realized I wasn't doing this dynamic datasource correctly even though it was working earlier, since there was only single instance of FacilityInstanceDatasource created upon the first request. Thanks a lot for your help.

dkrantsberg avatar Mar 25 '19 18:03 dkrantsberg