0

I have a problem when using the router.navigate.Before i added the router code my code worked well but now its giving me this error "ERROR TypeError: Cannot read property 'init' of undefined" this is my code:

this is the constructor:

export class IndexedDBService {
constructor(
private _store: Store<AppState>,
private _electron: ElectronService,
private _device: Device,
private userService: UtilizadoresService,
private workTypesService: TiposTrabalhoService,
private modelTypesService: TiposModeloService,
private modelService: ModelosService,
private clientService: ClientesService,
private paymentMethodService: FormasPagamentoService,
private salesNotSent: VendasPorEnviarService,
private groupTypeWork: GruposTiposTrabalhoService,
private companies: CompanyService,
private _alertCtrl: AlertController,
private _translate: TranslateService,
private _router: Router,
) {}

this is when i call the router:

        this._router.navigate(['login']);

this is when i call my db.init function which is in my app.module:

export function dbInitializer(db: IndexedDBService) {
return () => db.init();
}

this is my db init function:

public async init(): Promise<string> {
console.warn(
  isDevMode() ? '[Activation Disabled] Running in dev mode!' : ''
);

const activationCode: string = localStorage.getItem(kActivation);
let compareCode: string;
if (this._electron.isElectronApp) {
  compareCode = encrypt(this._electron.ipcRenderer.sendSync('device-info'));
} else {
  compareCode = encrypt(
    `${this._device.manufacturer}|${this._device.model}|${this._device.uuid}`
  );
}

// bypass activation when in dev mode
if (activationCode === compareCode || isDevMode()) {
  try {
    const isDbCreated = await IdbHelper.idbCon.initDb(this.getDbSchema());
    if (isDbCreated === true) {
      await (this.overwriteDB());
      return 'database created';
    } else {
      await this.overwriteDB();
      return 'database opened';
    }
  } catch (error) {
    throw error.message;
  }
} else {
  return undefined;
}
}

Thank you for your help

6
  • Can you try dbInitializer(private db: IndexedDBService) { return () => this.db.init(); Commented Jan 2, 2020 at 12:24
  • it gives out this errors: A parameter property is only allowed in a constructor implementation because of the private and 'this' implicitly has type 'any' because it does not have a type annotation.ts because of this Commented Jan 2, 2020 at 12:27
  • Can you post the code where you're instantiating the IndexedDBService and actually calling the exported function dbInitializer()? Looks like you're not getting the IndexedDBService passed in to make the init() call. Commented Jan 2, 2020 at 12:42
  • this is the only place where i call dbInitializer providers: [ StatusBar, SplashScreen, { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, { provide: APP_INITIALIZER, useFactory: dbInitializer, deps: [IndexedDBService], multi: true, }, Printer, Device, ], Commented Jan 2, 2020 at 12:46
  • Thanks for including this. One thing I notice is in your providers array, you're not loading the IndexedDBService as a provider, therefore the token won't be available as a dependency for your APP-INITIALIZER provider. Can you add IndexedDBService to your providers array and see if that fixes the issue? Commented Jan 2, 2020 at 15:15

1 Answer 1

1

One thing I notice is in your providers array, you're not loading the IndexedDBService as a provider, therefore the token won't be available as a dependency for your APP_INITIALIZER provider. If you add IndexedDBService to your providers array that should clear things up.

  providers: [
    IndexedDBService, //added this
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    {
      provide: APP_INITIALIZER,
      useFactory: dbInitializer,
      deps: [IndexedDBService],
      multi: true
    },
    Printer,
    Device
  ],
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.