首页 > 解决方案 > Firestore 的离子问题“存储在 Firestore 中的 Date 对象的行为将会改变”

问题描述

我目前在使用firestore时遇到问题,由于某种原因,每当我编译时,我都会收到以下错误:

index.esm.js:77 [2018-09-13T17:35:48.498Z]  @firebase/firestore: Firestore (5.0.4): 
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:

我尝试按照建议进行操作并将其传递如下

apiKey: "**********", authDomain: "************", databaseURL: "************", storageBucket :“****************”,messagingSenderId:“****************”,projectId:“******** ***",

我仍然遇到同样的错误。似乎困扰我的另一个问题是这是唯一的问题,还是我的代码有问题。因为我不知道这个错误是否会阻止我的应用程序正常运行,所以我试图从我的 firebase 获取信息,但没有任何显示。通常页面显示错误,当 chrome 开发人员也显示错误时。在这种情况下,错误仅限于 chrome 开发人员选项卡。因此,我试图在 Firestore 中访问的数据没有显示出来。这是我的代码

联系人.ts

import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import firebase from 'firebase';
import {AngularFirestore} from 'firebase/firestore';

@Component({
  selector: 'page-contact',
  templateUrl: 'contact.html'
})
export class ContactPage {
  public dataJSON;
  public dataAux;

  constructor(public navCtrl: NavController,
    public alertCtrl: AlertController) {
     // afs.firestore.settings({ timestampsInSnapshots: true });
    let db = firebase.firestore();
    var auxint = 0;
    this.dataAux
    let auxString = '[';
    db.collection('teste').where("Deletado", "==", false).get().then(res => {
      res.forEach(item => {

        //pegue todos os dados do banco e crie um novo JSON com as informações de 
        auxint++;
        auxString += '{"id":"' + item.id + '","documento":' + JSON.stringify(item.data()) + '}';

        if (res.size != auxint)
          auxString += ', ';
      })
      auxString += ']';
      this.dataJSON = JSON.parse(auxString);

    }).catch(err => {
      console.log('Error: ' + err);
    });
  }
  doAlert(a: string) {
    const alert = this.alertCtrl.create({
      title: 'ID',
      subTitle: 'ID is: ' + a,
      buttons: [
        {
          text: 'Item has been deleted',
          handler: () => {
            let db = firebase.firestore();
            db.collection('teste').doc(a).update({ Deletado: true, DataUpdate: firebase.firestore.FieldValue.serverTimestamp() }).then(res => {
              const alert1 = this.alertCtrl.create({
                title: 'Deleted',
                subTitle: 'Youve deleted: ' + a,
                buttons: [
                  {
                    text: 'Deleted',
                    handler: () => {
                      location.reload();
                    }
                  }
                ]
              })
              alert1.present();
            }).catch(() => {
              console.log("An error occurred");
            })
          }
        },
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Buy clicked');
          }
        }
      ]
    });
    alert.present();
  }
}

联系人.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Consulta firebase
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <ion-list>
    <ion-list-header>Nomes armazenados do banco</ion-list-header>
    <ion-item>
      @ionicframework 
    </ion-item>
    <ion-item *ngFor="let data of dataJSON">
      <ion-icon name="ionic" item-start></ion-icon>

      <p>ID: {{data.id}}</p>
      <p>Data Criação: {{data.DataCriacao}}</p>
      <p>Data ultima atualizacao: {{data.DataUpdate}}</p>
      <p>Deletado: {{data.Deletado}}</p>
      <p>Nome: {{data.nome}}</p>
      <p>Numero: {{data.number}}</p>
      <button ion-button  block color="dark" (click)='doAlert(data.id)'>
        Alerta ID
      </button>
    </ion-item>
  </ion-list>

</ion-content>

标签: angularionic-frameworkionic3google-cloud-firestore

解决方案


这个问题的问题在于这一行auxString += '{"id":"' + item.id + '","documento":' + JSON.stringify(item.data()) + '}';,我忘记将我的“文档”文本更改为我用来测试我的查询的文本。


推荐阅读