【问题标题】:Want to fetch data from firestore to my ionic 4 app想要从 firestore 获取数据到我的 ionic 4 应用程序
【发布时间】:2019-11-08 15:15:44
【问题描述】:

我能够将一些数据上传到我的 Firestore 帐户,但我无法获取或读取数据。我是新手,如果我能像新手一样得到指导,我将不胜感激。

     ngOnInit() {
    this.bookingservice.read_AcBookings().then(data =>
     this.booking = data.map(e => {
      return {
        id: e.payload.doc.id,
        isEdit: false,
// tslint:disable-next-line: no-string-literal
        firstname: e.payload.doc.data()['firstname'],
// tslint:disable-next-line: no-string-literal
        lastname: e.payload.doc.data()['lastname'],
// tslint:disable-next-line: no-string-literal
        phonenumber: e.payload.doc.data()['phonenumber'],
// tslint:disable-next-line: no-string-literal
        address: e.payload.doc.data()['address'],
// tslint:disable-next-line: no-string-literal
        location: e.payload.doc.data()['location'],
// tslint:disable-next-line: no-string-literal
        date: e.payload.doc.data()['date'],
// tslint:disable-next-line: no-string-literal
        servicebooked: e.payload.doc.data()['servicebooked'],

      };
    }));
    console.log(this.booking);

  }

这是服务

 read_AppliancesBookings() {
  return new Promise<any>((resolve, reject) => {
    this.afAuth.user.subscribe(currentUser => {
    if (currentUser) {
this.snapshotChangesSubscription = this.firestore.collection('Bookings').doc(currentUser.uid).collection('Appliances Bookings')
.snapshotChanges();
resolve(this.snapshotChangesSubscription);
    }
  });
    });
}

【问题讨论】:

  • console.log(this.booking); 的输出是什么?
  • 一个未定义的结果,data.map 也不是一个函数

标签: ionic-framework google-cloud-firestore modal-dialog ionic4


【解决方案1】:

你的bookingservice有几个基本错误,所以我只是为你重写了。希望您可以通过查看此代码了解问题所在。

import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import { reject } from 'q';

@Injectable({
  providedIn: 'root'
})
export class bookingservice {

  constructor(public afAuth: AngularFireAuth, public firestore: AngularFirestore) { }

  userId = this.afAuth.auth.currentUser.uid;

  read_AppliancesBookings() {

        return new Promise((resolve, reject) => {
          this.firestore
            .doc<any>(`Bookings/${userId}`)
            .valueChanges()
            .subscribe(doc => {
              resolve(doc);
            });
        }).then((result) => {
          return result;
        });

  }


}

让我知道这是否有效。

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 2021-12-09
    • 2023-04-04
    • 2020-02-19
    • 2019-01-17
    • 1970-01-01
    • 2019-11-25
    • 2019-12-12
    相关资源
    最近更新 更多