【问题标题】:Angular and Ionic 4 to listen to live data Error: InvalidPipeArgument: '' for pipe 'AsyncPipe'Angular 和 Ionic 4 监听实时数据错误:InvalidPipeArgument: '' for pipe 'AsyncPipe'
【发布时间】:2019-01-10 04:22:58
【问题描述】:

我正在尝试使用 angularfire2 和 firebase 实时数据库实时更新我的​​ ionic 4 应用程序中的消息。代码如下所示

运行时抛出异常 错误:InvalidPipeArgument: '' for pipe 'AsyncPipe'

如果我删除了 async 这个词,那么它显示得很好,但是当我从另一个实例发布一条新消息时,整个数据都会重复。

html

<ion-list lines="full" style="background:transparent">
                    <ion-item  style="padding-top:10px;" *ngFor="let msg of messages | async">
                        <ion-row  style="width:100%;">
                            <ion-col size="3">
                                <ion-row>
                                    <ion-col class="sg-reg-text">{{formatName(msg.name)}}</ion-col>
                                </ion-row>
                                <ion-row>
                                    <ion-col style="padding:0;padding-left:8px" class="sg-tiny-text"><ion-icon name="time" color="light"></ion-icon>&nbsp;now</ion-col>
                                </ion-row>
                            </ion-col>
                            <ion-col style="border-bottom: 1px solid #7e7c8d;padding-bottom: 10px">
                                <ion-row>
                                    <ion-col class="sg-reg-text">{{msg.message}}</ion-col>
                                </ion-row>
                            </ion-col>
                        </ion-row>
                    </ion-item>
              </ion-list>

ts

messages:Post[]
refresh(){
this.messages = []
this.sgSvc.getMessages().subscribe(
  (rsp:any) => {
                  for(let data of rsp){
                    console.log("hehe:" + JSON.stringify(data))
                    this.messages.push(new Post(data._name, data._message, data._uid))
                  }

                }
)
  }

ngOnInit() {
this.refresh()
}

svc.ts

private msgList: AngularFireList<Post>

getMessages(){

this.msgList = this.db.list<Post>('posts')
return this.msgList.snapshotChanges().pipe(
   map(changes => 
    changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
   )
  );
  }

【问题讨论】:

  • 这是因为messages 不是Observable&lt;T&gt;,它只是一个Array&lt;T&gt;,您将对象推送到。您不需要或不应该使用 async 管道和 messages
  • 如果我不这样做,那么我会遇到我提到的另一个问题

标签: angular ionic-framework firebase-realtime-database angularfire2 ionic4


【解决方案1】:

尝试以下使用 RxJS map() 运算符构建 Post 对象数组,而不是每次在 subscribe() 中将项目推送到 messages。当您使用当前代码删除 async 管道时,您会收到重复数据,因为您没有将数组重新初始化为空数组。无论哪种方式,您都不会使用带有Array&lt;T&gt;async 管道:

messages: Observable<Post[]>;

// ...

this.messages = this.sgSvc.getMessages().pipe(
  map((messages: any) => messages.map(m => new Post(m._name, m._message, m._uid)));
);

您也可以不使用async 管道:

messages: Post[] = [];

// ...

this.sgSvc.getMessages().subscribe((messages: any) => {
  this.messages = messages.map(m => new Post(m._name, m._message, m._uid));
});

希望对您有所帮助!

【讨论】:

  • 这让我陷入另一个错误:错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。
  • 你能确认messages 被输入为Observable&lt;Post[]&gt; 吗?您还可以通过临时订阅确认 map 运算符正在生成一个数组吗?
  • 您可以注销地图操作员正在生成的内容吗?你能确认它正在为Post 对象创建一个数组吗?或者在 HTML 中只使用json 管道而不是ngFor,仅用于故障排除。
  • 对不起,我怎么打印?在 map(messages:any) => 中放置 console.log 不起作用。另请注意,我的 svc.ts 代码仍然相同
  • 在您的 HTML 中只需执行 {{messages | async | json}},暂时删除 ngFor。您还可以在pipe() 内部使用tap RxJS 运算符,例如在外部map() 运算符之后使用tap(messages =&gt; console.log(messages)。我们只需要查看map() 正在创建的结构。您可以添加这是对您的问题的更新,以便更好地对其进行格式化/审查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2017-12-05
  • 1970-01-01
  • 2020-10-28
  • 2018-03-18
  • 1970-01-01
相关资源
最近更新 更多