0

When removing an item from my RTDB, my intention is to clean up user's likes if the item they like got removed.

With my data structure like so:

enter image description here

I am attempting to remove the nodes like this:

  const updates = {};

  //WORKS
  updates[`/strains/${title}/`] = null;
  updates[`/strainsMin/${title}/`] = null;

firebase
  .database()
  .ref('reviews')
  .orderByChild('s')
  .equalTo(title)
  .once('value', snapshot => {
    console.log(snapshot.val())
    snapshot.forEach(child => {
      console.log(child.key)
      //WORKS
      updates[`/reviews/${child.key}`] = null;
    });
  })
  .then(() => {
    firebase
    .database()
    .ref('userLikedStrains')
    .orderByChild(title)
    .equalTo(title)
    .once('value', snapshot => {
      console.log(snapshot.val() + 'likeylikeyyy')
      snapshot.forEach(child => {
        console.log(child.key)
        //DOESNT WORK
        updates[`/userLikedStrains/${child.key}/${title}`] = null;
      });
      firebase
      .database()
      .ref()
      .update(updates);
    })

For some reason this doesn't work. Is it because I am targeting a key (which is still a child?..)

I appreciate any help with this! Cheers.

hugger
  • 426
  • 4
  • 19

1 Answers1

0

The code you shared is not writing anything back to the database yet. For that you need to call update after the loop.

Something like:

let ref = firebase
    .database()
    .ref('userLikedStrains');

ref.orderByChild(title)
    .equalTo(title)
    .once('value', snapshot => {
      console.log(snapshot.val())
      snapshot.forEach(child => {
        console.log(child.key)
        updates[`/userLikedStrains/${child.key}/${title}`] = null;
      });
      ref.update(updates);
    })
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hey Frank, thanks for reaching out. I actually did have that in my code, whoops. I have updated my question. – hugger Mar 06 '21 at 01:40
  • You added an additional `then`, but aren't returning anything from the first `then`. Try what I have in my answer instead. – Frank van Puffelen Mar 06 '21 at 03:17
  • I've tried your suggestion but still no luck - I am wondering if it is because of the way my structure is? I mean I don't think so but since I have it nested on that second layer perhaps orderByChild doesn't apply to this? My reviews node is only one level deep and it works, but I figured since I structured the userLikedStrains node the way I did it would still be considered a child (AK-47) which is the title variable. – hugger Mar 06 '21 at 04:10
  • 1
    Good point: this definitely makes no sense: `ref.orderByChild(title)`. At the very least it should be `ref.orderByChild("title")`, since you want order on the `"title"` field. But in this case indeed, you'll need `ref.orderByChild("AK-47/title")`. If that `AK-47` is dynamic, see https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Mar 06 '21 at 04:25
  • .ref('userLikedStrains').orderByChild(`${title}/title`) was indeed the fix. Thanks so much for making that clear!! – hugger Mar 06 '21 at 04:38
  • Please also have a look at https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value, as it is likely that your query runs unindexed, on the client right now. – Frank van Puffelen Mar 06 '21 at 15:54