在角度应用和firebase集成中获取时间戳错误

时间:2018-04-25 18:43:39

标签: angular firebase angularfire2

我正在使用firebase开发一个Angular应用程序。

这是我得到的错误:

存储在Firestore中的Date对象的行为将会发生变化 你的应用程序可能会破裂。 要隐藏此警告并确保您的应用不会中断,您需要添加 在调用任何其他Cloud Firestore方法之前,将以下代码发送到您的应用:

 const firestore = firebase.firestore();
 const settings = {/* your settings... */ timestampsInSnapshots: true};
 firestore.settings(settings);

通过此更改,存储在Cloud Firestore中的时间戳将被回读为 Firebase Timestamp对象而不是系统Date对象。所以你也会 需要更新代码,期望Date改为期望时间戳。例如:

// Old:
const date = snapshot.get('created_at');
// New:
const timestamp = snapshot.get('created_at');
const date = timestamp.toDate();

启用新行为时,请审核Date的所有现有用法。在一个 未来的发布,行为将改变为新的行为,所以如果你不这样做 按照以下步骤操作,您的应用程序可能会突破。

这是我的package.json文件:

  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "angularfire2": "^5.0.0-rc.6.0",
    "core-js": "^2.4.1",
    "firebase": "^4.13.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "~1.7.4",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }

Firebase -v:4.13.1 AngularFire -v:5.0.0-rc.6.0 操作系统:Mac OSX / Windows(在两个平台上都试过。)

此外,这个错误背后的原因是什么?

1 个答案:

答案 0 :(得分:3)

我从 egaviriarestrepo 获得了修复 - https://github.com/angular/angularfire2/issues/1575

无论您何时调用AngularFirestore,都需要在构造函数中添加以下两行:

const settings = {timestampsInSnapshots: true};
afs.app.firestore().settings(settings);

看起来应该是这样的:

export class CoreModule {
  constructor( private afs: AngularFirestore) {
        const settings = {timestampsInSnapshots: true};
        afs.app.firestore().settings(settings);
  }
}

如果上述代码不起作用,请尝试以下代码:

export class CoreModule {
  constructor( private afs: AngularFirestore) {
        const settings = {timestampsInSnapshots: true};
        afs.firestore.settings(settings);
  }
}