Firebase APIから:
子の追加:このイベントは、この場所の最初の子ごとに1回トリガーされ、新しい子が追加されるたびに再度トリガーされます。
いくつかのコード:
listRef.on('child_added', function(childSnapshot, prevChildName) {
// do something with the child
});
しかし、この場所では子ごとに関数が1回呼び出されるので、実際に追加された子だけを取得する方法はありますか?
Firebase APIから:
子の追加:このイベントは、この場所の最初の子ごとに1回トリガーされ、新しい子が追加されるたびに再度トリガーされます。
いくつかのコード:
listRef.on('child_added', function(childSnapshot, prevChildName) {
// do something with the child
});
しかし、この場所では子ごとに関数が1回呼び出されるので、実際に追加された子だけを取得する方法はありますか?
以前のレコードをフェッチせずに、いくつかのチェックポイント以降に追加されたものを追跡するには、 と を使用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());
});
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());
});