angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.69k stars 2.19k forks source link

remove() on AngularFireList does not consider the filter applied #2989

Open SlyEzo opened 3 years ago

SlyEzo commented 3 years ago

Version info

Angular: 12.1.1

Firebase: 8.7.0

AngularFire: 6.1.5

Other (e.g. Ionic/Cordova, Node, browser, operating system): N/A

How to reproduce these conditions

Steps to set up and reproduce

  1. Create a list of objects with a "category" property
  2. Add items in the list
  3. Try to remove all objects having a certain value for the "category" property

Example of code

The following piece of code removes all objects regardless of the category property :

removeCategoryQuestions(currentCategory: string) {
  this.db
    .list<Evaluation>(
      `interview-questions/${this.type}`, 
      ref => ref.orderByChild('category').equalTo(currentCategory))
    .remove();
}

However, this piece of code displays only the objects having the right category value :

removeCategoryQuestions(currentCategory: string) {
  const filteredListRef = this.db
    .list<Evaluation>(
      `interview-questions/${this.type}`, 
      ref => ref.orderByChild('category').equalTo(currentCategory));
  filteredListRef.valueChanges().subscribe(evalu => console.log(evalu))
}

Expected behavior

.remove() called on a filtered AngularFireList should remove only the filtered objects.

Actual behavior

.remove() called on a filtered AngularFireList removes all objects, regardless of the filter applied.

SlyEzo commented 3 years ago

Update : I found that this code works, though the subscription on the AngularFirelist list notified once for each question removed :

removeCategoryQuestions(currentCategory: string) {
  const categoryEvaluations = (this.categoriesEvaluations[currentCategory] ?? []) as Evaluation[];
  categoryEvaluations
    .map(evaluation => evaluation.key)
    .filter(evaluationKey => evaluationKey != null)
    .forEach(evaluationKey => this.evaluationsRef.remove(evaluationKey));
}