-1

I am trying to update the node under promo id between apply and trigger location in firebase. it's not update same location where name already exists. It's disturb the ordering after update the name. I have tried method withupdate() but fail. Then setWithPrority() remove my other nodes(apply and trigger) and add only name node after update the name.Note data is already exist in firebase just update the name.

My script:

firebase.database().ref('/promotions/v1/' + site + '/'+ promoId).once('value').then(function (snapshot) {
  promo_data = snapshot.val();
  if (promo_data != null) {
    var refss = firebase.database().ref('/promotions/v1/' + site + '/'+ promoId );
    refss.child('/').update({"name":promoRuleName});
  }
});

My Output! enter image description here
Note: I have not trigger and apply node.I have only promoRuleName string.

Expected Output!

enter image description here.

Please help

Thanks in advance!

Raheel Aslam
  • 444
  • 1
  • 9
  • 28

2 Answers2

1

The ordering you see in the console is based on "priority-then-key". Since you called setWithPriority() the natural/alphabetical ordering is disturbed. I highly recommend not calling setWithPriority() anymore, the method is a relic from the past before Firebase supported orderByChild().

To fix the problem with your current data, you'll need to delete the priority from the node(s) where you have it. In the sample data you shared that can be accomplished with:

firebase.database().ref('/promotions/v1/22516/46/name').setPriority(null);

Also see: What does priority mean in Firebase?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

This script is work correctly for ordering between apply and trigger node.try this.

    firebase.database().ref('/promotions/v1/' + site + '/'+ promoId).once('value').then(function (snapshot) {
            promo_data = snapshot.val();
            if (promo_data != null) {
                var refss = firebase.database().ref('/promotions/v1/' + site + '/'+ promoId + '/');
                // First null the prority of apply node then update name.
                firebase.database().ref('/promotions/v1/' + site + '/'+ promoId + '/'+'apply/').setPriority(null);
                refss.update({'name': promoRuleName});
            }

        });

First I null the priority of apply node then update the name using update() method.

Raheel Aslam
  • 444
  • 1
  • 9
  • 28