[英]Exception on firebase.firestore.Timestamp toDate()
I get the exception when I try to access a firebase.firestore.Timestamp (trialEnds) on a document using:当我尝试使用以下方法访问文档上的 firebase.firestore.Timestamp (trialEnds) 时出现异常:
<ng-container *ngIf="account$ | async as account;">
// Listing out other account info works fine here...
// Then an exception on trialEnds access
<span *ngIf="account.trialEnds != null">
{{account.trialEnds.toDate() | date: 'M/d/yyyy'}})
</span>
The exception I get is like this:我得到的例外是这样的:
core.js:3828 ERROR TypeError: account_r9062.trialEnds.toDate is not a function at HeaderComponent_ng_container_14_ng_container_1_span_5_Template (header.component.html:38) at executeTemplate (core.js:7485) at refreshView (core.js:7362) at refreshDynamicEmbeddedViews (core.js:8428) at refreshView (core.js:7382) at refreshDynamicEmbeddedViews (core.js:8428) at refreshView (core.js:7382) at refreshDynamicEmbeddedViews (core.js:8428) at refreshView (core.js:7382) at refreshComponent (core.js:8492) core.js:3828 ERROR TypeError: account_r9062.trialEnds.toDate 不是 HeaderComponent_ng_container_14_ng_container_1_span_5_Template (header.component.html:38) at executeTemplate (core.js:7485) at refreshView (core.js:73coremicedView) 处的函数.js:8428) at refreshView (core.js:7382) at refreshDynamicEmbeddedViews (core.js:8428) at refreshView (core.js:7382) at refreshDynamicEmbeddedViews (core.js:8428) at refreshView (core.js:7382)在 refreshComponent (core.js:8492)
Here is how I set up the data in my component:以下是我在组件中设置数据的方法:
var trialDays = 14
var trialEndDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + trialDays)
var trialEnds = firebase.firestore.Timestamp.fromDate(trialEndDate)
var account: Account = {
...
trialEnds: trialEnds
}
The data shows up fine in the Firestore console, but the bizarre part is that if I update it in the console WITHOUT changing the data at all, I can read it just fine.数据在 Firestore 控制台中显示得很好,但奇怪的是,如果我在控制台中更新它而根本不更改数据,我可以很好地读取它。 There is literally no difference between the programatically set value and the manually updated value on the document:以编程方式设置的值和文档上手动更新的值之间实际上没有区别:
It also throws an exception on other calls like toMillis as well.它还会在其他调用(例如 toMillis)上引发异常。 What is going on?到底是怎么回事?
In your type file for account, is trialEnds definded as type of Timestamp在您的帐户类型文件中,将 trialEnds 定义为时间戳类型
import { Timestamp } from '@firebase/firestore-types';
export interface Account {
trialEnds: Timestamp;
}
I found a workaround.我找到了一个解决方法。 Instead of calling toDate on the timestamp, I create my own variable tied to the observable as:我没有在时间戳上调用 toDate ,而是创建了自己的与 observable 相关的变量,如下所示:
ngOnInit() {
try {
this.account$.subscribe(account => {
this.trialExpiration = account === null || account.trialEnds === null ?
null : new Date(account.trialEnds.seconds * 1000);
})
} catch (error) {
console.log(error)
}
}
Then in the template, use:然后在模板中,使用:
<span *ngIf="trialExpiration != null">
Trial Expires on {{trialExpiration | date: 'M/d/yyyy'}}
</span>
It works, but can anyone explain why calling seconds on the timestamp but toMillis or toDate throws?它有效,但谁能解释为什么在时间戳上调用 seconds 但 toMillis 或 toDate 会抛出?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.