我正在使用ionic 3和firebase firestore作为数据库进行项目。
但是,每当我进行CRUD操作时,我的firebase firestore都会显示警告。
index.esm.js:77 [2018-05-02T05:45:40.408Z] @firebase/firestore:
Firestore (4.13.0):
The behavior for Date objects stored in Firestore is going to change
AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to
add the
following code to your app before calling any other Cloud Firestore
methods:
const firestore = firebase.firestore();
const settings = {/* your settings... */ timestampsInSnapshots:
true};
firestore.settings(settings);
With this change, timestamps stored in Cloud Firestore will be read
back as
Firebase Timestamp objects instead of as system Date objects. So you
will also
need to update code expecting a Date to instead expect a Timestamp.
For example:
// Old:
const date = snapshot.get('created_at');
// New:
const timestamp = snapshot.get('created_at');
const date = timestamp.toDate();
Please audit all existing usages of Date when you enable the new
behavior. In a
future release, the behavior will change to the new behavior, so if
you do not
follow these steps, YOUR APP MAY BREAK.
答案 0 :(得分:10)
我在app.module.ts中添加了以下代码,然后出错。
firebase.initializeApp(environment.firebase)//加载firebase服务器设置
firebase.firestore()。settings({timestampsInSnapshots:true})
答案 1 :(得分:1)
我刚刚重新安装了“angularfire2”。 它已经解决了问题。
答案 2 :(得分:0)
我遇到了同样的问题,如果您使用Angular
和angularfire2
这可能会对您有所帮助:
如警告所示,您应该在app init时将timestampsInSnapshots
属性设置为true。您可以在AppComponent
的构造函数中调用它:
import { AngularFirestore } from 'angularfire2/firestore';
export class AppComponent {
constructor(db: AngularFirestore) {
const settings = { timestampsInSnapshots: true };
db.app.firestore().settings(settings);
}
}
为避免出现警告,您可以设置设置类型:
import { AngularFirestore } from 'angularfire2/firestore';
import * as firebase from 'firebase';
export class AppComponent {
constructor(db: AngularFirestore) {
const settings: firebase.firestore.Settings = { timestampsInSnapshots: true };
db.app.firestore().settings(settings);
}
}
AppModule
的构造函数也是调用此代码的好地方,它取决于您使用的内容。
答案 3 :(得分:0)
实施此设置后,日期将不再以javascript日期返回。它们将是Firestore时间戳。您的代码很可能会受到影响。我创建了以下函数来将任何对象的任何Timestamp属性转换为javascript Date属性。
private convertFirestoreTimestampsToDates(myObject: any): void {
for (let propertyName in myObject) {
if (myObject.hasOwnProperty(propertyName)) {
if (myObject[propertyName]) {
if (myObject[propertyName].__proto__.constructor.name === 'Timestamp') myObject[propertyName] = myObject[propertyName].toDate();
}
}
}
}