elpeyotl elpeyotl - 1 year ago 202
AngularJS Question

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'});

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


Answer Source

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
 } else {
    var rates = rateSnapshot.val(); //should be an array
    rates.push(1); //append new value to the array
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download