1

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...

1
  • is your current code working? Commented Jul 13, 2016 at 15:27

1 Answer 1

1

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);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

El maestro! This solved the problem! thanks! Didn't know it was so easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.