Пагинация Nuxt Firestore на страницы / Вытягивание дополнительных документов при прокрутке с помощью выборки - PullRequest
0 голосов
/ 08 мая 2020

Это моя текущая выборка nuxt в моем индексе. vue страница.

async fetch() {
const response = this.$fireStore
  .collection('recipes')
  .where('publish', '==', true)
  .orderBy('updated', 'desc')
  .limit(10)

try {
  await response.get().then((querySnapshot) => {
    const lastVisible = querySnapshot.docs[querySnapshot.size - 1]
    querySnapshot.forEach((doc) => {  
      this.articles.push({
           doc.data()
      }) 
    })
  })
} catch (e) {
  console.log('There is some error: ' + e)
  return
}  
console.log('Success')}

У меня есть метод, в котором ожидается отозвать $ fetch, когда он достигнет конца страницы.

 methods: {
    fetchMorePosts{
      if (iAmAboutToReachEnd) {            
          this.$fetch()
        }
   }        
  },

сейчас в моем следующем запросе я хочу передать дополнительный параметр в запрос startAfter (lastVisible)

как я могу этого добиться?

Я пытался сделать это, но получаю ошибка, говорящая, что ответ определен, но не используется (внутри блока if). Также не определен другой ответ об ошибке вне блока if.

 async fetch() {
 if (this.lastVisible === null){
 const response = this.$fireStore
  .collection('recipes')
  .where('publish', '==', true)
  .orderBy('updated', 'desc')
  .limit(10)
} else {
    const response = this.$fireStore
  .collection('recipes')
  .where('publish', '==', true)
  .orderBy('updated', 'desc')
  .startAfter(this.lastVisible)
 .limit(10)
}     


   try {
  await response.get().then((querySnapshot) => { same code goes here })}

Свойство данных для справки:

 data() {
    return {
      lastVisible: null,
      articles: []
    }
  }
...