2014-12-01 87 views
0

我無法從列表中刪除單個項目。我想刪除'最古老的'項目,並且通過.push()方法添加了這些項目。這樣做似乎很簡單,但我有問題。對於我的數據結構,請參見下文。我確信我只是在做一些愚蠢的事情,因爲這一定是一個常見的用例。從.push創建的列表中刪除最後一項()

任何想法/反饋將不勝感激。

代碼:

firebase.child('articlesList').orderByChild('site').equalTo('SciShow').limitToFirst(1).once('value', function(snapshot){ 

    // This was one try, This seems to remove the entire articleList 
    snapshot.ref().remove(); 


    // I have also tried this, and this seems to do nothing at all 
    snapshot.forEach(function(dataSnapshot){ 
    dataSnapshot.ref().remove(); 
    }); 
}); 

數據結構:

"articlesList" : { 
    "-Jc16JziK668LV-Sno0s" : { 
     "id" : "http://gdata.youtube.com/feeds/api/videos/c8UpIJIVV4E", 
     "index" : "SciShow", 
     "link" : "http://www.youtube.com/watch?v=c8UpIJIVV4E&feature=youtube_gdata", 
     "site" : "SciShow", 
     "title" : "Why Isn't \"Zero G\" the Same as \"Zero Gravity\"?" 
    }, 
    "-Jc16Jzkn6q41qzWw3DA" : { 
     "id" : "http://gdata.youtube.com/feeds/api/videos/Wi9i8ULtk4s", 
     "index" : "SciShow", 
     "link" : "http://www.youtube.com/watch?v=Wi9i8ULtk4s&feature=youtube_gdata", 
     "site" : "SciShow", 
     "title" : "The Truth About Asparagus and Your Pee" 
    }, 
    "-Jc16Jzkn6q41qzWw3DB" : { 
     "id" : "http://gdata.youtube.com/feeds/api/videos/J7IvxfcOkmM", 
     "index" : "SciShow", 
     "link" : "http://www.youtube.com/watch?v=J7IvxfcOkmM&feature=youtube_gdata", 
     "site" : "SciShow", 
     "title" : "Hottest Year Ever, and Amazing Gecko-Man Getup!" 
    }, 

回答

4

在火力地堡的鄉親對他們的谷歌集團回答了這個給我。我想我會張貼給其他人使用。 = = = 嗨瑞安,

你靠近!您不希望使用值事件,而是使用child_added事件。值事件將會在/ articlesList /節點上的所有數據被觸發一次。這就是爲什麼你看到它刪除整個列表。如果您使用child_added事件,它會爲每個孩子啓動。或者,如果你像你一樣限制它,它只會觸發一小部分兒童。還有一件事要改變,就是使用limitToLast(1)而不是limitToFirst(1)來獲取最後一個孩子。

下面的代碼:

firebase.child('articlesList').orderByChild('site').equalTo('SciShow').limitToLast(1).once('child_added', function(snapshot){ 
    snapshot.ref().remove(); 
}); 

雅各

相關問題