Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove object from Firebase using AngularFire 2

I am trying to create a delete function that will get the delete my object from the Firebase.

I have the following structure on FireBase:

recipes   recipebooks      recipesPerRecipebook
  - id          - id        - recipebook_id
  - id          - id           - recipe_id: true

What I want to do is when I click delete I want to remove the recipe_id from recipes and the remove it also from recipesPerRecipebook/${recipebook_id}

I created a function in my recipes.service.ts

deleteRecipe(recipe_id:string, recipebook_id:string) {
    this.sdkDb.child('recipes').remove(recipe_id)
      .then(
        () => alert('recipe deletion requested !')
      );

    this.sdkDb.child('recipesPerRecipebook/${recipebook_id}').remove(recipe_id)
      .then(
        () => alert('recipe Association deletion requested !')
      );
  }

I then have this in recipeDetails.component.ts

delete() {
    this.recipesService.deleteRecipe(this.recipe.$key, this.recipe.recipebook_id);
  }

I get the following error:

error_handler.js:54 Error: Firebase.remove failed: first argument must be a valid function.

I have two questions, firstly and most importantly where am I going wrong. And secondly doing this.sdkdb.child(... twice feels a bit wrong and cumbersome could I simplify this?

like image 365
Daimz Avatar asked Nov 17 '16 11:11

Daimz


1 Answers

Looks quite odd.

Please take a look at the official docs: https://github.com/angular/angularfire2/blob/master/docs/2-retrieving-data-as-objects.md#deleting-data

So you should be able to delete an object like this:

af.database.object('/--your-list-name--/--your-obejct-key--').remove();

where af is the injected AngularFire2 service.

like image 155
slaesh Avatar answered Oct 20 '22 01:10

slaesh