nbPages and page return undefined

I tried:

const client = algoliasearch('xxxxxxx', 'xxxxxxxxxx');
const index = client.initIndex('xxxxx');
await index
  .search(searchQuery, {
    ...requestOptions,
    length: 10,
    offset,
    ...
  })
  .then(({ hits, page, nbPages }) => {
    console.log("page, nbPages", page, nbPages)
    ...

I’m expecting to get back page and nbPages, but getting undefined for both.
What’s the right way to get those in the response?

Hey Algolia team, could I please have an answer for this? Thanks!

Hi there!

Those attributes should be part of the response by default indeed.
A few things come to mind as to why they might be missing:

  • You specify attributesToRetrieve in requestOptions and you don’t have nbPages and page in the array, in which case you should add them.
  • You have specified attributesToRetrieve in your index settings and you don’t have nbPages and page in the array, in which case you should either add them in the index settings or in the requestOptions at query time
  • You have specified those two attributes in the unretrievableAttributes index settings, in which case you should remove them from there

You can learn more about those two attributes from our docs:

I hope this helps!

Thanks for replying Vincent. I was using offset based pagination vs. page. That was the problem. I set it up so long ago that I forgot about it :slight_smile:

TIL, I didn’t even know we had this option :sweat_smile: so I missed it in your code snippet
I’m glad you got it figured out!

1 Like