Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use startAt() in Firebase query?

enter image description here

Let's suppose above firebase database schema.

What I want to is retrieve messages which after "15039996197" timestamp. Each of message object has a createdAt property.

How can I get messages only after specific timestamp? In this case, last two messages are what I want to retrieve.

I tried firebaseb.database().ref(`/rooms/$roomKey/messages`).startAt('15039996197') but failed. It return 0. How can I do this?

like image 826
ton1 Avatar asked Aug 29 '17 09:08

ton1


1 Answers

The case with your query is that it's expecting that the message node should have a number value to start with, in that case you want a child node with the name createdAt. So in that case you must specify that you want to order by createdAt, thus you need to do this

firebase.database().ref(`/rooms/$roomKey/messages`).orderByChild('createdAt').startAt('15039996197').on(//code);

This way it'll return all nodes inside message that has a child named createdAt an it starts at 15039996197. Ordering your query may be a little bad for performance, for that i sugest taking a look at .indexOn rule.

For more information take a look here.

Hope this helps.

like image 64
Gabriel Barreto Avatar answered Sep 28 '22 06:09

Gabriel Barreto