Как реализовать операцию удаления с помощью AngularFireStore - PullRequest
0 голосов
/ 27 февраля 2019

Я пытаюсь реализовать операцию DELETE в угловом приложении.Я хочу иметь возможность щелкнуть по кнопке и удалить документ пожарного депо, но мой код не работает.Поэтому я использую сервис для всех своих операций в пожарном депо и вызываю сервис в моем компоненте

station.service.ts

import { Injectable } from "@angular/core";
import { AngularFirestore } from "@angular/fire/firestore";
import { Station } from "../../models/station.model";

@Injectable({
  providedIn: "root"
})
export class StationService {
  constructor(private afs: AngularFirestore) {}

  deleteStation(stationId: string) {
    this.afs.doc("stations/" + stationId).delete();
  }
}

station.component.ts

import { Component, OnInit } from "@angular/core";
import { StationService } from "../services/station.service";
import { Station } from "./../../models/station.model";

@Component({
  selector: "app-station",
  templateUrl: "./station.component.html",
  styleUrls: ["./station.component.css"]
})
export class StationComponent implements OnInit {
  stations: Station[];

  constructor(private stationService: StationService) {}

  ngOnInit() {
    this.stationService.getStations().subscribe(data => {
      this.stations = data.map(e => {
        return {
          id: e.payload.doc.data(),
          ...e.payload.doc.data()
        } as Station;
      });
    });
  }

  delete(id: string) {
    if (confirm("Confirm delete operation")) {
      //console.log(id);
      this.stationService.deleteStation(id);
    }
  }
}

Я не могу найти идентификатор в своем сообщении console.log, похоже, что это

address: "Somewhere along PTI road, Effurun. Delta State"
name: "Effurun"
region: "South-South"

Как я могу исправить свой код?Оповещение, я впервые работаю с пожарным магазином.

1 Ответ

0 голосов
/ 02 марта 2019

Наконец-то нашли ошибку.В методе station.component.ts ngOnInit () идентификатор был установлен для объекта вместо идентификатора.Установите это значение равным id так:

ngOnInit() {
  this.stationService.getStations().subscribe(data => {
    this.stations = data.map(e => {
      return {
        id: e.payload.doc.id,
        ...e.payload.doc.data()
      } as Station;
    });
  });
}

Теперь будет возвращаться фактический идентификатор типа string вместе с каждым объектом, который затем может быть передан в метод delete(id: string).

...