Search code examples
loopbackjs

How to get the app instance within the controller?


I started using Loopback4 on a small app I am making.

The app is periodically retrieving data from an external site and the data is stored in a variable in the app object.

Now I'm a bit stuck as I don't know how to get the app instance within the controller. Do I inject it? Please help.


Solution

  • You can inject the application object using the binding key CoreBindings.APPLICATION_INSTANCE.

    import {CoreBindings, inject} from '@loopback/core';
    import {MyApplication} from '../application';
    
    export class MyController {
      constructor(
        @inject(CoreBindings.APPLICATION_INSTANCE)
        private app: MyApplication,
      ) {}
    
      // ...
    }
    

    Documentation: https://loopback.io/doc/en/lb4/apidocs.core.corebindings.application_instance.html