首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将snapshotChanges()与防火墙和离子4结合使用

如何将snapshotChanges()与防火墙和离子4结合使用
EN

Stack Overflow用户
提问于 2019-02-18 17:20:15
回答 1查看 2.5K关注 0票数 0

嗨,我的朋友们,我在我的ionic4应用程序中有代码可以从消防局检索数据,我尝试了这段代码,但是它没有显示任何这些数据。

我尝试在我的代码中使用snapshotChanges(),但是它失败了,我还想检索文档的id,我如何做到这一点?

下面是我的代码:

news.page.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import {AngularFirestore, AngularFirestoreDocument} from 'angularfire2/firestore';
import {Observable} from 'rxjs';
import { Router } from '@angular/router';
import 'rxjs/add/operator/map';
export class FilmsPage implements OnInit {
  news: Observable<any[]>;
  constructor(public db: AngularFirestore, private router: Router) { }

  ngOnInit() {
      this.db.collection('123').snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    });
}

news.page.html

代码语言:javascript
复制
<ion-content padding>
        <ion-item *ngFor=" let count of news | async">
          <ion-button routerLink="/details/{{count.id}}">{{count.name}} -> id: {{count.id}}</ion-button>

</ion-item>
</ion-content>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-18 17:38:20

您的实现目前有几个问题。

第一个问题是,需要将this.db.collection('123').snapshotChanges()...的结果分配给news: Observable<any[]>类属性,以便能够在模板中有效地使用async管道:

代码语言:javascript
复制
ngOnInit() {
  this.news = this.db.collection('123').snapshotChanges().map(actions => {
    return actions.map(a => {
      const data = a.payload.doc.data();
      const id = a.payload.doc.id;
      return { id, ...data };
    });
});

下一个问题取决于您的RxJS版本。如果您的项目使用的是RxJS 5.5+,则应该使用可贯通算子。这将包括更新map运算符的导入,以及更新与snapshotChanges()一起使用它的方式。实际上,它只是将map()移动到pipe()

代码语言:javascript
复制
import { map } from 'rxjs/operators';

// ...

ngOnInit() {
  this.news = this.db.collection('123').snapshotChanges().pipe(
    map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    })
  );
});

希望这能帮上忙!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54752366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档