Firebase child_added只能添加子项 [英] Firebase child_added only get child added

查看:107
本文介绍了Firebase child_added只能添加子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Firebase API:

From the Firebase API:


小孩加入:此事件会在此位置为每个初始小孩
触发一次,并且每次添加一个新的
子元素时都会被触发。

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().limit(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆