0

I have the following problem where I am trying to get cookies from my browser however I need to get them as they appear. Some of them don't appear instantly so I am trying to use the below code to get them as they come. The error I am seeing in my editor is:

Property 'subscribe' does not exist on type '{}'

import { CookieService } from 'ngx-cookie-service';

this._cookieService.getAll().subscribe((result) => {
  console.log(result);
});

Can someone please assist

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
skydev
  • 1,867
  • 9
  • 37
  • 71

3 Answers3

2

Corresponding to the documentation the getAll function returns a map with the key value pairs. So no need to subscribe to it.

https://www.npmjs.com/package/ngx-cookie-service

var result = this._cookieService.getAll();

If you want to get notified of cookie changes you might end up writing a function that periodically checks for changes like suggested in other answers here or on other questions around: can i be notified of cookie changes in client side javascript

ngx-cookie-service doesnt provide any helper to reach that (nor does any of the other cookie helper libraries I found by a little research).

Simon Echle
  • 286
  • 2
  • 9
2

ngx-cookie-service's getAll() method returns object not Observable.

It is: getAll(): {}

Simply use:

result = this._cookieService.getAll();

Check Documentation

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
1

You could write a service with a polling function based on the rxjs timer(), that checks the cookies continuously:

 private timer$: Subscription = new Subscription();

 public polling(frequency = 1000 * 60 * 5) {
        this.timer$.unsubscribe();
        this.timer$ = timer(0, frequency).subscribe(
            () => this.result = this._cookieService.getAll();
        );
    } 

I hope this approach helps you!