2

i am facing problems with understanding more about on and once in firebase API. Assume I have this DB structure enter image description here When I listen for child_added, it works as expected.

firebase.database().ref(this.getRootRef())
        .orderByChild('createdAt')
        .limitToLast(NUM_MSG_PER_REQUEST)
        .on('child_added', (datas) => {
        if (datas) {
            _callback(datas.val());
        }
    });

But when I would to filter with once, nothing effected.

firebase.database().ref(this.getRootRef())
        .orderByChild('createdAt')
        .endAt('createdAt', '2019-03-17 02:46:50') // tested with startAt, equalsTo => the same result. No effect
        // .limitToLast(NUM_MSG_PER_REQUEST)
        .once('value', (vals) => {console.log(vals.val()});

Could someone explain more about these 2 functions and in this case, what should I write my could to have filters when doing once ?

Thanks a lot!

Update on 03/18/2019

The function getRootRef() returns '/messages/2019-03-15'.

Both code snippet work, but what I am not clear here is how the filters applied.

For example: orderByChild('createdAt')

With this filter, the first one return results which are ordered by 'createdAt'. But the second one will not. It return with no order.

Muhammad usman
  • 521
  • 1
  • 3
  • 16
Lap Tran
  • 89
  • 5
  • What are you doing with the return value of `once()`? Your code is apparently doing nothing with the promise it returns. – Doug Stevenson Mar 17 '19 at 23:49
  • Also, I will say that if you want to observe the difference between `on` and `once`, then you should apply them to the same query and see how they're different. If `once` yields no data, then `on` will yield no data as well. – Doug Stevenson Mar 17 '19 at 23:50
  • Doug points out two problems, but there's more that is not clear to me. I did notice that you have `on('child_added' and `once('value'`, so both will be called with a different type of snapshot. Aside from that, what does `this.getRootRef()` refer to? I highly recommend starting over, and making sure that we can compare apples to apples, and that the code you share is minimal, complete and can run standalone. See [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) for more tips on why and how to do that. – Frank van Puffelen Mar 17 '19 at 23:55
  • I would also like to add that the only difference between `on` and `once` is that `once` will listen for one event and then stop listening, while `on` is always be listening for the event, no matter how many times its called. – dotconnor Mar 18 '19 at 00:16
  • Thanks for your answers, I just updated to make it more clear. What I want in the second query it get the earlier message of date '2019-03-18' base on 'createdAt' field (this field should contain date-time). But it seems that filter not effected. – Lap Tran Mar 18 '19 at 02:04
  • The issue was resolved, I was wrong in my second query. The correct filter is endAt( '2019-03-17 02:46:50'). Not include the field name. Then it works as expected. – Lap Tran Mar 18 '19 at 03:17
  • 1
    Darn... I completely missed that while reading the code. Good catch Lap Tran! You might want to post it as an answer, so others can possibly benefit from it in the future. – Frank van Puffelen Mar 18 '19 at 03:56

0 Answers0