Search code examples
angulartypescriptrxjsangularfire2angularfire5

AngularFire2 Memory Leak after updating list


I'm trying to update a list (increment a value in the list) in firebase after a button is clicked, however, whenever I click on the button that value keeps incrementing without stopping unless I close the page tab.

onRecipeUpvote(recipe: Recipe)
{
    // get list with -> name: recipe.name
    let listRecipe = this.ngFireDB.list<Recipe>('/recipes', ref => ref.orderByChild('name').equalTo(recipe.name));

    listRecipe.snapshotChanges().map(actions => {
      return actions.map(action => ({ key: action.key, ...action.payload.val() }));
    }).subscribe(items => {
      return items.map(item => {
        // Increment its current upvotes by 1
        listRecipe.update(item.key, { upvotes: item.upvotes + 1 });           
      });
    });  
}

Solution

  • Try manually unsubscribing the Subscription after the function completes.