Firebase child_ added 只添加孩子 [英] Firebase child_added only get child added
本文介绍了Firebase child_ added 只添加孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自 Firebase API:
From the Firebase API:
Child 添加:此事件将为每个初始孩子触发一次在这个位置,每次有新的时候都会再次触发添加了孩子.
Child Added: This event will be triggered once for each initial child at this location, and it will be triggered again every time a new child is added.
一些代码:
listRef.on('child_added', function(childSnapshot, prevChildName) {
// do something with the child
});
但是由于该函数为这个位置的每个孩子调用一次,有没有办法只获取实际添加的孩子?
But since the function is called once for each child at this location, is there any way to get only the child that was actually added?
推荐答案
要跟踪自某个检查点以来添加的内容而不获取以前的记录,您可以使用 endAt()
和 limit()
获取最后一条记录:
To track things added since some checkpoint without fetching previous records, you can use endAt()
and limit()
to grab the last record:
// 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());
});
这篇关于Firebase child_ added 只添加孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文