3

我正在使用什么

  • 火库

我想要达到的目标

  • 处理文档数据

  • 将返回的日期转换为可读的内容

是)我有的

  • 我有一个专辑列表,我在其中使用snapshotChanges返回映射数据

  • 我有一张AngularFirestorDocument专辑

问题

  • 如何像在专辑列表组件中一样操作专辑组件中的返回日期?

专辑列表

所以这个组件正确地返回了一个专辑列表,我可以在返回它之前操纵日期,以便 HTML 显示正确的格式。

export class AlbumsListCompoment implements OnInit {

  private albumCollection: AngularFirestoreCollection<any>;
  albumns: Observable<any[]>;
  folderId: string;

  constructor(
    private readonly afs: AngularFirestore,
    private activatedRoute: ActivatedRoute,
    private router: Router
  ) {

    // Look at the url for the Folder ID and set the local variable
    this.activatedRoute.params.forEach((urlParameters) => {
      this.folderId = urlParameters['folderId'];
    });

    // Album Reference "folders/folderid/albums"
    this.albumCollection = afs.collection<any>(`folders/${this.folderId}/albums`);

    // Get the data
    this.albumns = this.albumCollection.snapshotChanges().map(actions => {
      return actions.map(a => {

        const data = a.payload.doc.data();

        // Get the date from each album
        var albumDateTimeStapm = data.album_date;

        // Convert the unix value and format it based on the users locale setting
        var albumDateISO = moment(albumDateTimeStapm).format("DD/MM/YYYY");

        // Create a new 'name' to use in the HTML binding
        data.formattedDate = albumDateISO;

        const id = a.payload.doc.id;
        return { id, ...data };
        });
       });
      }

  ngOnInit() {

  }

  }

专辑

我并没有完全展示如何接近这个组件来执行相同的操作。它不是一个列表,它是一个文档/专辑。

export class AlbumDetails implements OnInit {

  folderId: string;
  albumId: string;

  private albumDoc: AngularFirestoreDocument<any>;
  album: Observable<any>;

  constructor(
    private readonly afs: AngularFirestore,
    private activatedRoute: ActivatedRoute,
    private router: Router) {

    // Look at the url for the Folder and Album ID and set the local variable
    this.activatedRoute.params.forEach((urlParameters) => {
      this.folderId = urlParameters['folderId'];
      this.albumId = urlParameters['albumId'];
    });

    this.albumDoc = afs.doc<any>(`folders/${this.folderId}/albums/${this.albumId}`);
    this.album = this.albumDoc.valueChanges();


  }

  ngOnInit() {}

}

任何帮助将不胜感激 :)

4

1 回答 1

3

我不是 100% 确定我理解你的问题。据我了解,您希望获取文档返回数据的句柄,然后在显示在 HTML 上之前对其进行更改。

如果是这种情况,您可以执行以下操作:

this.albumDoc = afs.doc<any>(`folders/${this.folderId}/albums/${this.albumId}`);
this.album = this.albumDoc.valueChanges();
this.album.subscribe(value => {
  const value1 = value.value1;
  const value2 = value.value2;
  ...
  const valueN = value.valueN;
});

如果您还需要返回值的元数据(例如 id),请执行以下操作:

this.albumDoc = afs.doc<any>
(`folders/${this.folderId}/albums/${this.albumId}`);
  this.album = this.albumDoc.snapshotChanges();
  this.album.subscribe(value => {
    const id = value.payload.id;
    const value1 = value.payload.data().value1;
    const value2 = value.payload.data().value2;
    ...
    const valueN = value.payload.data().valueN;
});

有关这方面的更多信息,请参阅angularfire2 文档

于 2017-10-06T12:01:08.167 回答