ButterCMS/buttercms-js

Pagination issue

sereginserhii opened this issue · 2 comments

I tried to implement fetch one-by-one blog posts and got an interesting issue with the page_size property of the butter.post.search options object. My call chain looks something like this:

import Butter from 'buttercms'

let fetchPageNumber = 0

const fetchBlog = () => {
  if (!fetchPageNumber) return 

  Butter(process.env.NEXT_PUBLIC_BUTTER_CMS_API_KEY)
    .post
    .search({ page: fetchPageNumber, page_size: 1 })
    .then(({ data }) => {
      console.log(data)
      fetchPageNumber = data.meta.next_page
    })
}

In data.meta.count I got 4 items, but when I reached the 3rd page it cast an error: Invalid page \"3\": That page contains no results.. When I changed page_size to 2, it works as expected, also if I passed something like a: 'a' into the search method to make it look like this: ... .search({ page: fetchPageNumber, page_size: 1, a: 'a' }), it started works as expected too 😅.

Main technology: Next v13

@sereginserhii I was able to reproduce it. The problem is that you pass your options as a first argument to the search function. As you can see in the SDK implementation, the first argument of the search function must be the query itself, and the second argument is optional and there should be your pagination parameters { page: fetchPageNumber, page_size: 1 }.

Your code says that { page: fetchPageNumber, page_size: 1 } is the query that should be used for the search.

It's also described in the readme: https://github.com/ButterCMS/buttercms-js#blog-engine

post
search(query[, params])

So you have to change your code:

.post.search({ page: fetchPageNumber, page_size: 1 })

to

.post.search(query, { page: fetchPageNumber, page_size: 1 })

If you want to "fetch one-by-one blog posts" so you can use the .post.list function that doesn't need the query argument.

.post.list({ page: fetchPageNumber, page_size: 1 })

@prokopsimek @sereginserhii closing this issue for now, as it appears to be related to implementation