从.push()创建的列表中删除最后一项

时间:2014-12-01 19:50:44

标签: firebase

我无法从列表中删除单个项目。我想删除“最旧的”项目,这些项目已通过.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!"
    },

1 个答案:

答案 0 :(得分:4)

Firebase上的人们在他们的Google Group上为我解答了这个问题。我想我会张贴其他人使用。 = = = 嘿瑞安,

你很亲密!您不想使用值事件,而是使用child_added事件。使用/ articlesList / node中的所有数据将触发value事件一次。这就是为什么你看到它删除整个列表。如果您使用child_added事件,则会为每个孩子触发。或者,如果你像你一样限制它,它只会触发一部分子项。要改变的另一件事是使用limitToLast(1)而不是limitToFirst(1)来获取最后一个孩子。

以下是代码:

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

雅各

相关问题