60

Firebase APIから:

子の追加:このイベントは、この場所の最初の子ごとに1回トリガーされ、新しい子が追加されるたびに再度トリガーされます。

いくつかのコード:

listRef.on('child_added', function(childSnapshot, prevChildName) {
    // do something with the child
});

しかし、この場所では子ごとに関数が1回呼び出されるので、実際に追加された子だけを取得する方法はありますか?

4

5 に答える 5

44

以前のレコードをフェッチせずに、いくつかのチェックポイント以降に追加されたものを追跡するには、 と を使用endAt()limit()て最後のレコードを取得できます。

// retrieve the last record from `ref`
ref.endAt().limitToLast(1).on('child_added', function(snapshot) {

   // all records after the last continue to invoke this function
   console.log(snapshot.name(), snapshot.val());

});
于 2012-08-03T07:28:12.113 に答える
38

limit()メソッドは非推奨です。limitToLast()そしてlimitToFirst()メソッドがそれを置き換えます。

// retrieve the last record from `ref`
ref.limitToLast(1).on('child_added', function(snapshot) {

   // all records after the last continue to invoke this function
   console.log(snapshot.name(), snapshot.val());
   // get the last inserted key
   console.log(snapshot.key());

});
于 2014-12-23T13:24:37.870 に答える