Firestore的离子问题“ Firestore中存储的Date对象的行为将改变”

时间:2018-09-13 18:01:26

标签: angular ionic-framework ionic3 google-cloud-firestore

我目前使用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:“ *****************”,     essagingSenderId:“ *************”,     projectId:“ ***********”,

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

contact.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();
  }
}

contact.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>

1 个答案:

答案 0 :(得分:0)

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