Child_added获取最后添加的项目javascript [英] Child_added get last item added javascript

查看:27
本文介绍了Child_added获取最后添加的项目javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Firebase云功能,我正在尝试添加最后一个子项,以继续执行该项的任务.我的结构是这样的:

For my Firebase cloud functions, i'am trying to get the last child item added, in the aim to proceed a task for this item. My structure is like this:

  -Tips
      -TipsId
             -Safe
                  -SafeId
                  -SafeId
                   etc

我的目标是添加最后一个子级SafeId项目.我找到了此代码段,但问题是,当删除SafeId项目时也会触发该事件.

My goal is to get the last child SafeId item added. i found this snippet, but the problem is that it is also trigger when item SafeId is remove.

ref.orderByChild('timestamp').startAt(Date.now()).on('child_added',function(snapshot) {
  console.log('new record', snapshot.val());
});

推荐答案

要获取最后一项,只需 limitToLast():

To get just the last item, just limitToLast():

ref.orderByChild('timestamp').limitToLast(1).on('child_added',function(snapshot) {
  console.log('new record', snapshot.val());
});

如果您没有 timestamp 属性,但已使用 push()添加项目,则也可以使用 orderByKey():

If you don't have a timestamp property, but have used push() to add the items, you can also use orderByKey():

ref.orderByKey().limitToLast(1).on('child_added',function(snapshot) {
  console.log('new record', snapshot.val());
});

这篇关于Child_added获取最后添加的项目javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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