简体   繁体   中英

Child_added get last item added javascript

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

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());
});

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

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

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());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM