2016-07-13 34 views
1

我想將值推送到存儲在我的firebase數據庫中的嵌套數組。目標看起來是這樣的:在firebase中創建嵌套數組對象

rateData is the array i want to push new data

我查詢數據庫火力和接收快照。有了這個快照,我想存儲我的更改。如果我把新的價值它火力創建我:

Firebase after push

如何避免獨特的FB ID和一個正常的索引計數器更換呢?

我的代碼從角服務:

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

有沒有辦法讓快照爲$ firebaseobject,操縱它,然後將它與$保存到火力地堡DB救?

謝謝...

+0

是你當前的代碼的工作? – adolfosrs

回答

1

dbRef.push()總會有新的UID關鍵數據追加。 改爲使用dbRef.update()dbRef.set()

例(未經測試)

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); 
} 
+0

El大師!這解決了問題!謝謝!不知道這很容易。 – elpeyotl