'TypeError: xxService.init is not a function - App_Initializer

I am trying to setup an app_initializer in my application's app.module.ts. However, I have no idea why I'm getting this error: service.init() is not a function when I try to reload my application. What am I doing wrong?

@NgModule({
  declarations: [AppComponent, navigatableComponents],
  entryComponents: [],
  imports: [
    Modules
  ],
  providers: [
   Service,
   Service1,
    {
      provide: APP_INITIALIZER,
      useFactory: appInitializer,
      deps: [
           Service,
           Service1,
      ],
      multi: true
    },
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent],
})
export function appInitializerFactory(service: Service,
                                      service1: Service1): () => Promise<any> {
  return () => Promise.all([
     service.init(),
  ]).then(() => {
     return service1.init();
  })
}


Solution 1:[1]

After some digging I figured out why this was happening.

In my service.ts file, I was calling the capacitor plugin from constructor which was wrong and which created the module error as it cant be injected to a component and needs to be declared as a constant as per docs.

old service.ts

constructor( private appInfo: AppInfo,
                private deviceInfo: DeviceInfo,
                private platform: Platform) {
    }

    public async init(): Promise<void> {
        ConstantService.vCode = "" + this.appInfo.version;
        ConstantService.vNumber = this.appInfo.build;
        ConstantService.Platform = this.deviceInfo.platform;
        ConstantService.OSVersion = this.deviceInfo.osVersion;
    }

new service.ts

    constructor(private platform: Platform) {
    }

    public async init(): Promise<void> {
        const info = await Device.getInfo();
        const appInfo = await App.getInfo();

        ConstanService.vCode = "" + appInfo.version;
        ConstantService.vNumber = appInfo.build;
        ConstantService.Platform = info.platform;
        ConstantService.OSVersion = info.osVersion;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shapebuster