Firestore 获取 Javascript 日期而不是时间戳

Firestore getting Javascript Date instead of Timestamp

我正在使用 Angular 6Firestore 开发一个应用程序,我将我的日期保存为 Javascript 的 Date 对象。问题是,当我执行 snapshotChanges() 时,它 returns 一个 Unix Timestamp 而不是我数据库中的 Date 对象。

我在线搜索了解决方案 (this, and this) 并找到了 Firestore 的这个 配置选项

{ timestampsInSnapshots: false }

我正在尝试像这样在我的应用程序组件中应用它:

constructor(private _db: AngularFirestore) {
    _db.firestore.settings({ timestampsInSnapshots: false });
  }

但后来我得到这个错误:

Firestore has already been started and its settings can no longer be changed. You can only call settings() before calling any other methods on a Firestore object.

我也曾尝试在调用 snapshotChanges() 之前更改设置,但这对行为也没有任何影响。

注意:我也在使用 AngularFirestoreModule.enablePersistence()

更新:

我试过 this 但它给了我这个错误:

@firebase/firestore: Firestore (5.0.3): The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.

任何建议都很好。

嗯,请尝试调用 _db.app.firestore().settings({}),因为您正在将应用范围扩大到 changes/configurations。并且由于您已经提到您需要 enablePersistence() 的离线支持,在这种情况下尝试在您的组件中调用它,而不是根模块,但是在调用 settings() 之后。

在你的组件中:

constructor(private _db: AngularFirestore) {
    _db.app.firestore().settings({ timestampsInSnapshots: false });
    _db.app.firestore().enablePersistence();
}

通过将以下代码添加到我的应用程序控制中,我能够实现所需的功能。

constructor(private _db: AngularFirestore) {
  _db.firestore.settings({ timestampsInSnapshots: false });
  _db.firestore.enablePersistence();
}

记住,如果您打算使用上述代码,请不要AngularFirestoreModule.enablePersistence()。只需添加 AngularFirestoreModule 就足够了。