首页 > 解决方案 > 错误 toDate() InvalidPipeArgument: '无法转换时间戳.. Firebase

问题描述

我在 Firestore 中有以下字段: 在此处输入图像描述

当我尝试在我的应用程序中获取它时,例如:

<p>{{ fechaInicio | date }}</p> 得到以下错误:

ERROR 错误:InvalidPipeArgument:'无法将管道'DatePipe'的“时间戳(秒=1553230800,纳秒=0)”转换为日期'

然后我看到添加toDate()据说可以解决问题:

<p>{{ fechaInicio.toDate() | date }}</p>

这正确显示了日期,但是我收到以下错误:

错误类型错误:无法读取未定义的属性“toDate”

我该如何解决?

标签: angularfirebasegoogle-cloud-firestore

解决方案


似乎变量“fechaInicio”是异步加载的。如果是这样,“fechaInicio”在更新之前保持未定义。因此,您可以做的是隐藏 html 段落,直到数据加载到变量中。

尝试以下修改。

<p *ngIf="fechaInicio">{{ fechaInicio.toDate() | date }}</p>

推荐阅读