Tony
Tony

Reputation: 783

Update Data in Firebase With VueJS

I want to update a value under a certain key without using the "naive" way of copy/pasting the key value. Because I want it to scale to any client.

I basically want to update on click the "edit" property to "true".

Here's my current tree structure: enter image description here

My current method:

      var query= db.ref('Clients/AgenceEco/forms/step1/');
          query.update({
              edit: true
      })

The problem is that it actually adds an edit property under the "step1", like the following: enter image description here

I obviously don't want that but want to update the value under the random generated key of firebase.

How would you go about solving this issue? Thanks

Upvotes: 0

Views: 2839

Answers (2)

camden_kid
camden_kid

Reputation: 12813

I think you can do this:

var updates = {};
updates['Clients/AgenceEco/forms/step1/edit'] = true;
db.ref().update(updates);

https://firebase.google.com/docs/database/admin/save-data#section-update

Edit: Regarding your comments you could do this (but I think there may be an easier way of doing it):

var ref = 'Clients/AgenceEco/forms/step1';
firebase.database().ref(ref).once('value', function(snapshot) {
    var data = snapshot.val();
    if (data) {
        data.edit = true;
        firebase.database().ref(ref).update(data);
    }
}

Upvotes: 1

user6748331
user6748331

Reputation:

Just do that on child, not step1 directly. With child method:

var query= db.ref('Clients/AgenceEco/forms/step1/')
query.child().update({
  edit: true
})

Upvotes: 0

Related Questions