0

The code below works as intended when removing just one child, however I need to remove multiple children, in my case 6. See attached screenshot. How would I go about adding all 6 children to the remove function?

deleteActivity= () => {
    const { currentUser } = firebase.auth();
    firebase
      .database()
      .ref(`/users/${currentUser.uid}/data/${this.state.dateForFirebase}`)
      .remove('ActivityStart1');      
  }

enter image description here

ericm21
  • 75
  • 1
  • 11

2 Answers2

1

Hi you could pass an object with the keys to remove seted to null through an atomic update.

try this and let me know if it works

deleteActivity= () => {
    const { currentUser } = firebase.auth();
    const ref = firebase.database().ref(`/users/${currentUser.uid}/data/${this.state.dateForFirebase}`)
    var atomicUpdate = {};
    atomicUpdate["ActivityAvgHR1/"] = null;
    atomicUpdate["ActivityCalories/"] = null;
    atomicUpdate["ActivityDurationHrs1/"] = null;
    atomicUpdate["ActivityDurationMns1/"] = null;
    atomicUpdate["ActivityMaxHR1/"] = null;
    atomicUpdate["ActivityStart1/"] = null;

    ref.update(atomicUpdate)

  }
Helmer Barcos
  • 1,898
  • 12
  • 18
0

From the firebase documentation:

Delete data The simplest way to delete data is to call remove() on a reference to the location of that data.

You can also delete by specifying null as the value for another write operation such as set() or update(). You can use this technique with update() to delete multiple children in a single API call.

Check this question and update the paths with null values.

Mukeyii
  • 520
  • 3
  • 13