【问题标题】:Firebase Firestore showing warningFirebase Firestore 显示警告
【发布时间】:2018-10-12 04:18:27
【问题描述】:

我正在做一个项目,使用 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.

【问题讨论】:

  • 嗯,警告是准确地显示出了什么问题以及如何解决它......

标签: firebase ionic3 google-cloud-firestore


【解决方案1】:

我在 app.module.ts 中添加了以下代码,然后错误更正。

firebase.initializeApp(environment.firebase) // 加载 firebase 服务器设置

firebase.firestore().settings( { timestampsInSnapshots: true })

【讨论】:

  • 这在我更新了 firebase nodejs cfg 后起作用,以解决此错误:“Admin SDK 无法为 Firestore 设置设置”。感谢:stackoverflow.com/a/51449990/2162226。顺便说一句,我打算编辑邮政编码行以使用代码样式(只有 1 个缩进),但不想打扰您应用的粗体设置。我认为如果格式化会更容易阅读和处理。好的指针,感谢这里的解决方案
【解决方案2】:

我刚刚重新安装了“angularfire2”。 问题已经解决了。

  • npm 卸载 angularfire2
  • npm install angularfire2@5.0.0-rc.9

发布版本信息:https://github.com/angular/angularfire2/releases

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,如果您使用 Angularangularfire2 这可能会对您有所帮助:

    如警告所示,您应该在应用初始化时将 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的构造函数也是调用这段代码的好地方,用什么看你自己。

    【讨论】:

    • 在最新版本的 Angularfire2 5.0.0-rc.8.1-next/5.0.0-rc.8 中,官方支持设置 Firebase 设置,默认情况下,timestampsInSnapshots 设置为 true。
    【解决方案4】:

    实施此设置后,日期将不再以 javascript Dates 的形式返回。它们将是 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();
          }
        }
      }
    }
    

    【讨论】:

      【解决方案5】:

      为了增强来自“farkasnorbert90”的答案,我在 app.component.ts 中使用了 afs.firestore.settings 而不是 db.app.firestore()

      import * as firebase from 'firebase';
      import { AngularFirestore } from 'angularfire2/firestore';
      
        constructor(
          public auth: AuthService,
          afs: AngularFirestore,
        ) {
          const settings: firebase.firestore.Settings = {
            timestampsInSnapshots: true,
          };
          afs.firestore.settings(settings);
        }
      

      【讨论】:

      • 在最新版本的 Angularfire2 5.0.0-rc.8.1-next/5.0.0-rc.8 中,官方支持设置 Firebase 设置,默认情况下,timestampsInSnapshots 设置为 true。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2012-01-24
      • 2017-11-13
      相关资源
      最近更新 更多