Patricio Vargas
Patricio Vargas

Reputation: 5522

Cannot read property of undefined in APP_INITIALIZER - Angular

I'm trying to execute my saveURL method with the APP_INITIALIZER, but I'm getting and error in my saveURL method.

ERROR TypeError: Cannot read property 'setCookie' of undefined

App.modulte.ts

 ...
   export function saveUrl(dualLogonService: DualLogonService) {
     console.debug('Executing saveUrl');
      return (): Promise<any> => {
        dualLogonService.setCookie(); //THIS IS WHAT GIVES ME ERROR
        return null;
      };
    }
   ...
    providers: [
    ...
          {
              provide: APP_INITIALIZER,
              useFactory: saveUrl,
              deps: [DualLogonService],
              multi: true
          }
    ]

DualLogonService.ts

import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { AuthenticationService } from '@sec/security';
import { appConfigMapping } from 'src/environments/app.config.mapping';
import { of } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class DualLogonService implements CanActivate {
...
  constructor(
    private router: Router,
    private authService: AuthenticationService
  ) {}
..

  setCookie(): void {
    document.cookie = 'Deeplink' + '=' + this.router.url + ';path=/';
  }

Upvotes: 1

Views: 2449

Answers (1)

dotnet-provoke
dotnet-provoke

Reputation: 1608

Add DualLogonService to the Providers-array

Upvotes: 1

Related Questions