简体   繁体   English

firebase.firestore.Timestamp toDate() 上的异常

[英]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.

相关问题 以反应形式将 firebase.firestore.timestamp 转换为 patchValue 之前的日期 - Convert firebase.firestore.timestamp to date before patchValue of data in a reactive form 不能使用 Firestore 时间戳(&#39;toDate&#39; of undefined) - Can't use Firestore timestamp('toDate' of undefined) 如何在Firestore.FieldValue.Timestamp上使用toDate()? - How to use toDate() on Firestore.FieldValue.Timestamp? Firebase timestamp.toDate() 不是基金函数,无法读取未定义的属性“toDate” - Firebase timestamp .toDate() is not a fundction & Cannot read property 'toDate' of undefined" 如何正确地“ toDate()”大型文档的Firestore时间戳? - How to “toDate()” Firestore timestamp's of large documents properly? Firestore Timestamp 和 Google Cloud Functions 的问题(date.toDate() 不是函数) - Problem with Firestore Timestamp and Google Cloud Functions (date.toDate() is not a function) firebase/firestore 中的时间戳 - timestamp in firebase/firestore Firebase 格式化日期的 firestore 时间戳 - Firebase firestore timestamp to Formatted Date firebase.firestore.Timestamp.fromDate() 未定义 - firebase.firestore.Timestamp.fromDate() is undefined 从 Cloud Firestore 在 MatDatepicker 中加载时间戳 | 火力基地 - Load timestamp in MatDatepicker from Cloud Firestore | Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM