0

I wanted to implement infinite scroll to load more data in my app. I wanted to fetch the data from latest to oldest i.e from bottom to top , so I have used limiToLast()

I have followed Implementing Infinite Scrolling with Firebase? to achieve this functionality.

Here is my query function

var lastKey = '';
var myData = [];

async fetchData(){
    try{
        var snap = await firebase.database().ref(category).orderByKey().endAt(lastKey).limitToLast(5).once('value');
        console.log(snap);
        snap.forEach(childSnap => {
            myData.push(childSnap.val();
            lastKey = childSnap.key;
        });
    }catch(err){
        //handle errors
    }
}

But the snap is null if I console.log it. what am I doing wrong? I want to fetch 5 items at a time from the bottom if I use the same code using startAt() and limitToFirst() to fetch 5 items at a time it works fine:

var lastKey = '';
var myData = [];

async fetchData(){
    try{
        var snap = await firebase.database().ref(category).orderByKey().startAt(lastKey).limitToFirst(5).once('value');
        snap.forEach(childSnap => {
            myData.push(childSnap.val();
            lastKey = childSnap.key;
        });
    }catch(err){
        //handle errors
    }
}
boomboxboy
  • 2,384
  • 5
  • 21
  • 33

0 Answers0