2016-04-06 19 views
0

如何將對象中的項目作爲嵌套標籤保存(如同索引號一樣)。Firebase - 我怎樣才能保存每個對象的項目由一個子彈,反對他們的索引號?

例如即時通訊與後續數據的工作:

{ 
    "all_items": [ 
    { 
     "id": 900, 
     "item_id": 20, 
     "name": "First Item", 
     "slug": "first-item" 
    }, 
    { 
     "id": 800, 
     "item_id": 21, 
     "name": "Second Item", 
     "slug": "second-item" 
    } 
    ] 
} 

保存它,像這樣:

var dataRef = new Firebase("https://<firebase-Id>.firebaseio.com/"); 
var data= {"all_items": [....]};  // This is the truncated object above. 
dataRef.update(data); //saving 

在火力節省通過其索引的每個值在這裏看到:

enter image description here

我怎樣才能具有第一項的被保存作爲其項蛞蝓第一項
和第二項(索引號)保存爲第二項等上。

到目前爲止,我已經與快照像這樣的工作:

dataRef.child('all_items')once("value", function(snapshot) { 
    snapshot.forEach(function(childSnapshot) { 

    //KEY 
    var key = childSnapshot.key(); 
    console.log('key: ', key); // returns 0 then 1 

    //VALUE 
    var childData = childSnapshot.val(); 
    console.log('childData: ', childData); //returns first item then second 
    console.log('slug: ', childData.slug);//returns 'first-item' then 'second-item' 

     //issue is here. I would like to use 
    key.update(childData.slug) 
    //but firebase returns "TypeError: key.update is not a function 

    }); 

現在,我可以登錄他們,我怎麼能救他們,我通過它的塞覆蓋索引號?

回答

0

我這樣做是通過創建一個循環分配每個到路內的火力點參考的新實例是毛坯:

 for (var i in data.all_items) { 

      singleObj = data.all_items[i]; 
     console.log('one item:',singleObj); 

     var newDataRef = new Firebase("https://<firebase-id>.firebaseio.com/all_items/"+ val.slug+'/'); 
     newDataRef.update(singleObj) 

     } 
相關問題