G
G
Ghoulll2020-02-05 11:01:54
Angular
Ghoulll, 2020-02-05 11:01:54

Why is the error 'Cannot read property 'subscribe' of undefined' thrown?

There is a service that sends data to the chrome extension and receives data, but when you try to subscribe to an Observable, ERROR takes off, the service code is:

getCerts(): Observable<any> {
    const editorExtensionId = 'asdfghjkl';
    const message = {
      cmd: 'SomeMSG',
      args: 'SomeARR',
    };
    let result = null;
    chrome.runtime.sendMessage(editorExtensionId, message, res=> {
      if (res=== 0) {
        alert('...');
        return 0;
      }
      if (res.error !== 0) {
        if (res.error === -1) {
          alert('...');
        }
        return 0;
      }

      if (res.certsCount === null || res.certsCount === 0) {
        alert('...');
        return 0;
      }
      result = res;
      return result;
    });
      return result
  }


Component code:
...
  <app-auth (getCertificates)="getCertificates()" [certs]="cert$ | async"></app-auth>
...
  cert$: Observable<any>;
...
  getCertificates(): void {
    this.authService.getCerts().subscribe(p => this.cert$ = p);
  }
...


And the stupid wrapper code:
...
  <button id='connect-button' (click)="onGetCertificates($event)">Получить что-то</button>
  <br>
  <div id="response" *ngFor="let cert of certs">
    {{cert | json}}
  </div>
....
  @Input() certs: Observable<any>;
  @Output() getCertificates: EventEmitter<any> = new EventEmitter<any>();
  onGetCertificates($event): any {
      this.getCertificates.emit($event);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-02-05
@Ghoulll

getCerts(): Observable<any> {
  // ...
  return new Observable(observer => {
    chrome.runtime.sendMessage(editorExtensionId, message, res=> {
      // ...
       observer.next(result);
       observer.complete();
    })
}
})

Homework - shorten code using https://rxjs.dev/api/index/function/bindCallback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question