Angular FIrebase 5 objects keys not being displayed. So can't delete

后端 未结 2 1079
说谎
说谎 2020-12-03 23:05

The question has been answered but I\'m looking for a, um, more straightforward one if available. It seems strange that we\'d have to implement not one but two mappings just

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 00:03

    to create an interface:

    export interface Client{
        key?: string;
        firstName?: string;
        lastName?: string;
        email?: string;
        phone?: string;
        balance?:number;
    
    }
    
    import { Injectable } from '@angular/core';
    import { AngularFireDatabase, AngularFireList, AngularFireObject} from '@angular/fire/database';
    import { Observable } from 'rxjs';
    import { map } from 'rxjs/operators';
    
    
    
    @Injectable()
    export class ClientService {
      client: AngularFireList;
      clients: Observable;
    
      constructor(public db: AngularFireDatabase) {
        this.client = db.list('/clients');
        this.clients = this.client.snapshotChanges().pipe(
          map(res => res.map(c => ({ key: c.payload.key, ...c.payload.val() 
        }))
    
       ));
    
      }
    
      getClients(){
        return this.clients;
      }
    
    
    }
    
    import { Component, OnInit } from '@angular/core';
    import { ClientService } from '../../services/client.service';
    import { Client} from '../../models/client'
    
    
    
    @Component({
      selector: 'app-clients',
      templateUrl: './clients.component.html',
      styleUrls: ['./clients.component.css']
    })
    export class ClientsComponent implements OnInit {
      clients:Client[];
    
      constructor(
        public clientService:ClientService
      ) { }
    
      ngOnInit(){
        this.clientService.getClients().subscribe(clients=>{
          this.clients = clients;
          console.log(this.clients);
        })
    
    
      }
    
    }
    

提交回复
热议问题