0

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 });           
      });
    });  
}
Muizz Mahdy
  • 798
  • 2
  • 8
  • 24

1 Answers1

1

Try manually unsubscribing the Subscription after the function completes.

Isurendrasingh
  • 432
  • 7
  • 20
  • Unsubscribing didn't work, however, I have solved it by allowing upvoting only once, because the memory leak happens after upvoting twice (I still cannot figure out why though). – Muizz Mahdy Apr 16 '18 at 21:29