如何从Firebase DB Ionic和Firebase检索/获取数据

时间:2018-08-25 19:28:46

标签: angular firebase firebase-realtime-database ionic3

我正在尝试使用Ionic和Firebase进行原始操作。到目前为止,我已经能够在Firebase Db中创建记录。我正在尝试检索要显示的列表。

我的代码如下:

/ homepage.ts /

export class HomePage {
  userList$: Observable<any[]>;

  constructor(public navCtrl: NavController, private _userService: UserListService) {

  }
ionViewDidLoad(){
   this.getUser();
 }


  public user: User = {
    FirstName: 'text',
    Lastname: 'Kumar',
    Email: 'amanGandey@gmail.com',
    PanNm: 'AmAnsjfdgs',
    AdhaarNm: '12311223411'
  }

  addUser() {
    this._userService.addUser(this.user).then(ref => {
      console.log(ref.key)
    })
  }

  getUser() {
    this.userList$ = this._userService.getUserList().snapshotChanges().map(changes => {
      return changes.map(c => ({
        key: c.payload.key, ...c.payload.val()
      }))
    })

  }
}

/ Service.ts /

import { Injectable } from "@angular/core";
import { AngularFireDatabase } from "angularfire2/database";
import { User } from '../../models/user/user.model';

@Injectable()
export class UserListService{
private userListRef = this.db.list<User>('user-list');

    constructor(private db: AngularFireDatabase){

    }
    getUserList(){
        return this.userListRef;
    }
    addUser(user: User){
        return this.userListRef.push(user);
    }
}

/ HTML /

 <ion-list>
    <ion-list-header>
      Users
    </ion-list-header>
    <ion-item *ngFor = "let user of userList$ | async"></ion-item>
    {{user.FirstName}}
  </ion-list>

/ 模型 /

export interface User {
    key?: string;
    FirstName: string;
    Lastname: string;
    Email: string;
    PanNm: string;
    AdhaarNm: string;
}

我无法从Firebase DB检索用户列表。 任何指导将不胜感激

0 个答案:

没有答案