错误错误:InvalidPipeArgument:'[object Object]'管道'AsyncPipe',即使返回一个可观察的对象

问题描述 投票:0回答:1

我发现了几个标题相同的问题,据我所知,其中一些建议该解决方案基本上返回一个Observable而不是一个数组(其他关于FireBase的问题不是我的情况)。好吧,据我所知,下面的代码确实返回了一个Observable(请看“ getServerSentEvent():Observable {return Observable.create ...”)

我的最终目标是从Rest WebFlux返回的流中获取所有事件。我没有回过头来,因为我非常确定问题与Angular中的某些错误有关。

最重要的是,我可以调试并查看来自app.component.ts的Extratos $事件(请参见下面的图像)。

整个日志

core.js:6185 ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
    at invalidPipeArgumentError (common.js:5743)
    at AsyncPipe._selectStrategy (common.js:5920)
    at AsyncPipe._subscribe (common.js:5901)
    at AsyncPipe.transform (common.js:5879)
    at Module.ɵɵpipeBind1 (core.js:36653)
    at AppComponent_Template (app.component.html:8)
    at executeTemplate (core.js:11949)
    at refreshView (core.js:11796)
    at refreshComponent (core.js:13229)
    at refreshChildComponents (core.js:11527)

app.component.ts

import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';
import { SseService } from './sse.service';
import { Extrato } from './extrato';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  providers: [SseService],
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  //extratos: any;
  extratos$ : Observable<any>;

  constructor(private appService: AppService, private sseService: SseService) { }

  ngOnInit() {
    this.getExtratoStream();
  }

  getExtratoStream(): void {
    this.sseService
      .getServerSentEvent("http://localhost:8080/extrato")
      .subscribe(
        data => {
          this.extratos$ = data;
        }
      );
  }
}

sse.service.ts

import { Injectable, NgZone } from '@angular/core';
import { Observable } from 'rxjs';
import { Extrato } from './extrato';

@Injectable({
  providedIn: "root"
})
export class SseService {
  extratos: Extrato[] = [];
  constructor(private _zone: NgZone) { }

  //getServerSentEvent(url: string): Observable<Array<Extrato>> {
  getServerSentEvent(url: string): Observable<any> {
    return Observable.create(observer => {
      const eventSource = this.getEventSource(url);
      eventSource.onmessage = event => {
        this._zone.run(() => {
          let json = JSON.parse(event.data);
          this.extratos.push(new Extrato(json['id'], json['descricao'], json['valor']));
          observer.next(this.extratos);
        });
      };
      eventSource.onerror = (error) => {
        if (eventSource.readyState === 0) {
          console.log('The stream has been closed by the server.');
          eventSource.close();
          observer.complete();
        } else {
          observer.error('EventSource error: ' + error);
        }
      }

    });
  }
  private getEventSource(url: string): EventSource {
    return new EventSource(url);
  }
}

app.component.html

<h1>Extrato Stream</h1>
<div *ngFor="let ext of extratos$ | async">
  <div>{{ext.descricao}}</div>
</div>

已填写可观察到的额外证据的证据

enter image description here

html angular rxjs server-sent-events ngzone
1个回答
1
投票

[编写此observer.next(this.extratos);时,这意味着this.extratos是您在回调的data参数中的组件侧得到的,所以当您执行此this.extratos$ = data;时,您实际上是在存储extratos [C0 ]。 TypeScript对此没有抱怨,可能是因为当您像从头开始构建Array时,它不够聪明,无法推断类型。

尝试一下:

Observable

并且在模板中:this.extratos$ = this.sseService .getServerSentEvent("http://localhost:8080/extrato");

© www.soinside.com 2019 - 2024. All rights reserved.