首页 > 解决方案 > 管道日期角度“无法转换”时间戳“

问题描述

我正在尝试使用有角度的管道,特别是以下内容:{{fechaCreacion | date: 'medium'}}并且我收到以下错误:Unable to convert Timestamp (seconds = 1528157765, nanoseconds = 878000000)" into a date 'for pipe' DatePipe'

这是我在 Firestore 中的注册: 在此处输入图像描述

当我离开{{ fechaCreacion }}时,它向我显示以下内容:

在此处输入图像描述

我该如何解决?

我在用着:

角 6

“angularfire2”:“^5.0.0-rc.10”

“火力基地”:“^5.0.4”,

组件.ts

  aviso: any = {};
  id;
  titulo;
  descripcion;
  fechaCreacion;
  categoria;

  constructor( private fs: FirebaseService, private activatedRoute: ActivatedRoute) { }

  ngOnInit() {
    this.id = this.activatedRoute.snapshot.params['id'];
    this.fs.getAvisoObject(this.id).valueChanges().forEach(aviso => {
      this.titulo= aviso.titulo,
      this.descripcion = aviso.descripcion,
      this.fechaCreacion = aviso.fechaCreacion,
      this.categoria = aviso.categoria
    });
  }

组件.html

<mat-card class="px-3 px-md-5 py-3 py-md-4 rounded">
    <div class="row no-gutters small pb-2 mb-3 d-flex justify-content-between border-bottom text-muted">
      <div>
        <span>{{ categoria }}</span>
      </div> 
      <div>
        <span>{{ fechaCreacion | date : 'medium' }}</span>
      </div>
    </div>
    <h2 class="font-weight-bold">{{ titulo }}</h2>
    <h5 class="font-weight-light">{{ descripcion }}</h5>
  </mat-card>

服务.ts

getAvisoObject(id: string) {
this.avisoObject = this.afs.doc('avisos/' + id);
return this.avisoObject;
}

标签: angularfirebasegoogle-cloud-firestore

解决方案


您必须在管道之前调用toDate()将 Firebase Timestamp 转换为 Javascript Date 对象,例如:

{{ creationDate.toDate() | date: 'medium' }}


推荐阅读