Home > Software engineering >  Angular Firestore duplicates output on console.log
Angular Firestore duplicates output on console.log

Time:09-03

I am trying to get the id of the selected data, and I am testing it out on the console.log but it always output 2 times. This photo here shows the output of the console.log.

enter image description here

I only want it to get the id once, then I will transfer it to another collection. The method that I'm doing is archive instead of delete. I have successfully copied it to another table but it gets duplicated that's why I'm only testing it first on the console.

While these are my code for when I click the delete button

employee.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { Observable } from 'rxjs';
@Injectable({
     providedIn: 'root'
})

export class EmployeeService {
     constructor(private firestore: AngularFirestore) { }

     deleteEmployee(id: string): Promise<any> {
          return this.firestore.collection('employees').doc(id).delete();
      }
}

list-employees.component.ts

deleteEmployee(id: string) {
this._employeeService.getEmployee(id).subscribe(data => {
  console.log(id);
   })
}

Please help, and let me know if I need some necessary codes to show, thank you!

CodePudding user response:

Try adding first or take(1):

deleteEmployee(id: string) {
  this._employeeService.getEmployee(id).pipe(first()).subscribe(data => {
    console.log(id);
  });
}

Otherwise you will continue to listen to changes to the employee after deletion.

  • Related