Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - limitToLast(5), then load previous data

I have a chat program. When the application loads, I load the last 5 messages by doing something like this:

var chatRef = new Firebase("https://xxxx.firebaseio.com/chat/");
  $scope.query = chatRef.orderByChild("Datestamp").limitToLast(5);
  $scope.chatList = $firebaseArray($scope.query);

I want to give the user the option to load more previous messages. But I am not sure how I can load the next 5 previous messages. I was trying something like this but with no success:

$firebaseArray(chatRef.orderByChild("Datestamp").endAt($scope.chatList[0].Datestamp).limitToFirst(5)).$loaded(function (data) {
  data.forEach(function (d) {
    $scope.chatList.push(d);
  });
});

So essentially my data set looks like this:

Msg 1

Msg 2

Msg 3

Msg 4

Msg 5

Msg 6

Msg 7

Msg 8

Msg 9

Msg 10.

When I load the app, I say limitToLast(5), which pulls:

Msg 6

Msg 7

Msg 8

Msg 9

Msg 10

Now I want to load the 2 previous records,

Msg 4

Msg 5

How can I accomplish this?

like image 264
Carsten Avatar asked Sep 26 '22 00:09

Carsten


1 Answers

You're looking for endAt(). You need to pass in the value of the first message that you got, so in your case the Timestamp of message 6.

 chatRef.orderByChild("Datestamp").endAt(message6.Timestamp).limitToLast(3);

You'll be getting message 6 in the results, which you already displayed. So you'll need to skip that last message from the results.

like image 119
Frank van Puffelen Avatar answered Sep 30 '22 07:09

Frank van Puffelen