1

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=/';
  }

1 Answer 1

1

Add DualLogonService to the Providers-array

Sign up to request clarification or add additional context in comments.

4 Comments

I tried that before and I got this error: Uncaught Error: Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule
Evidently you are injecting other services (which are not shown in the code you posted) that have dependencies (http or other services is the same). Try to follow some tutorials to understand how it works, like this: intertech.com/Blog/…
I have updated my questions with more code. So what I understood is that I have to add the other services I'm injecting in the constructor to the providers in the app.module.ts?
I would abstract away everything from the duallogonservice to a InitAppService having only the setcookie method in that service and you should be good to go!

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.