Pagination works fine unless last page

I have a pagination system with Fauna and it works fine, …except for last page. On the last page, data is missing the document referenced by after

Image the following data

  • A
  • B
  • C

A request { size: 2, after: B } returns [C] instead of [B, C] and that bug happens only in last page. All other pages work fine

Could you provide us with more information?
What we need to know to reproduce this is the query and indexes that you might be using to run it.

@Pierre_Drouillet After in Paginate is always inclusive. I am not able to reproduce what you have reported.

Paginate(Documents(Collection("offers")))

{
  data: [
    Ref(Collection("offers"), "278825721915245062"),
    Ref(Collection("offers"), "278825804038668806"),
    Ref(Collection("offers"), "278828527295398406")
  ]
}

Paginate(Documents(Collection("offers")),{size:2,after: [Ref(Collection("offers"), "278825804038668806")] })

{
  before: [Ref(Collection("offers"), "278825804038668806")],
  data: [
    Ref(Collection("offers"), "278825804038668806"),
    Ref(Collection("offers"), "278828527295398406")
  ]
}

Sure, I’ll try to abstract:

First of all the use case:

I want to paginate incrementally (eg, starting from page 1, then page 2, etc.)

The FQL looks more or less like this:

async function paginate(size, after, before) {
  const { after, before, data } = await new Client({ secret }).query(
      q.Map(
        q.Paginate(
          q.Join(
            q.Match(q.Index("members_search_by_shopName"), shopName),
            q.Match(q.Index("members_search_by_firstName_asc_lastName_asc"))
           ),
          { size, after, before }
        ),
        q.Lambda(
          ['firstName', 'lastName', 'ref'],
          q.Get(q.Var('ref')),
        ),
      ),
    );
  return { after, before, data }
}

As you can see, when I paginate, I first call this function with only size (no after or before since it is the first call).
Paginate returns with [after, before] that I can use for subsequent paginations. Said subsequent paginations working fine … until last page where [after] is omitted from data

Thanks

Do you always pass both before and after for every page? If so, perhaps the problem is that, for the last page where after is not defined, you are seeing the result of specifying before.

I always pass either after or before (or none, in case of first page)

Like @Jay-Fauna , I cannot reproduce your problem.

The pagination tutorial in the documentation includes establishing a collection called Letters which contains one document per alphabet letter, with a custom id for each starting with 101: Index tutorials | Fauna Documentation

I get the expected behaviour:

> Paginate(
  Documents(Collection("Letters")),
  { size: 2, after: Ref(Collection("Letters"), "125") }
)
{
  before: [ Ref(Collection("Letters"), "125") ],
  data: [
    Ref(Collection("Letters"), "125"),
    Ref(Collection("Letters"), "126")
  ]
}

Note that the pagination options object in your example always contains after and before. You should include one or the other, but not both, in your pagination queries. If you had, you would receive an error.

> Paginate(
  Documents(Collection("Letters")),
  {
    size: 2,
    after: Ref(Collection("Letters"), "125"),
    before: Ref(Collection("Letters"), "125")
  }
)
Error: invalid expression
{
  errors: [
    {
      position: [],
      code: 'invalid expression',
      description: 'No form/function found, or invalid argument keys: { paginate, size, after, before }.'
    }
  ]
}

Since you’re not getting an error, there might be a problem in the logic that picks the references to use for before and after.

Can you try creating the pagination options object based on whether the after or before parameters are defined?

Also, note that it should be an error to redeclare function parameters as constants.

The docs say:

It looks like the values for the relevant Index go ['firstName', 'lastName', 'ref'].

Are you pasing the firstName and lastName as well as the Ref? Or just the Ref?

I guess it wouldn’t make sense that this would be the issue if all the middle pages are working…