elpeyotl
elpeyotl

Reputation: 372

Creating nested array in firebase Object

I want to push a value to a nested array which is stored in my firebase database. The target looks like this:

rateData is the array i want to push new data

I'm query the firebase database and receive snapshot. With this snapshot I want to store my changes. If i push the new value it creates me in firebase this:

Firebase after push

How can I avoid the unique FB id and replace it with a normal index counter?

My code from the angular service:

        //Save StarBewertung on Mamis
        this.saveStarOnMami = function (data) {
            var ref = new Firebase("https://incandescent-heat-5149.firebaseio.com/contacts")

            var query = ref.orderByChild("id").equalTo(data).on("child_added", function(snapshot) {

                if( snapshot.val() === null ) {
                    /* does not exist */
                } else {
                    //snapshot.ref().update({"phone_fixed": '123'});
                    snapshot.ref().child('rateData').push(1);
                }
            });
        }

Is there a way to get the snapshot as an $firebaseobject, manipulate it, and then save it to the Firebase DB with $save?

Thanks...

Upvotes: 1

Views: 1335

Answers (1)

Timo
Timo

Reputation: 1814

dbRef.push() will always append the data with a new UID as key. Use dbRef.update() or dbRef.set() instead.

Example (untested)

 var rateSnapshot = snapshot.child('rateData');
 if( rateSnapshot.val() === null ) {  // rateData key does not exist
    rateSnapshot.ref().set([5]);
 } else {
    var rates = rateSnapshot.val(); //should be an array
    rates.push(1); //append new value to the array
    rateSnapshot.ref().set(rates);
 }

Upvotes: 1

Related Questions