How to paginate index which has terms and value?

I have this index:

{
  name: "reverse_entries_sort_user_created",
  unique: false,
  serialized: true,
  source: "entries",
  terms: [
    {
      field: ["data", "userRef"]
    }
  ],
  values: [
    {
      field: ["data", "createdAt"],
      reverse: true
    },
    {
      field: ["ref"]
    }
  ]
}

Then in my React app, I want to load fetch more data. I pass the id of the last doc id I have fetched in my app. But it does not work as I expected, it is returning the same data.

q.Map(
      q.Paginate(
        q.Match(
          q.Index('reverse_entries_sort_user_created'),
          '310621334632661191'
        ),
        {
          size: 2,
          after: [q.Ref(q.Collection('entries'), lastCreated)],
        }
      ),
      q.Lambda(['data.cratedAt', 'ref'], q.Get(q.Var('ref')))
    )
  )

How to do it properly?

Hi,

Can you give us more details on the 'after’ cursor being returned by the server and how you are passing it back to fetch more data? More information on the output you are seeing would be helpful too.

Thanks for your reply. Let me show you some results. I am using the index I have already shared (one term, one value, reversed, dorted by createdAt). Now let me show what correct results I got if I fetch 4 documents without after and then I will share results if returning 2 docs and after then another 2 using after ref. The id key is the ref of the doc.
Returning 4 docs at once(without after)= correct results:

[
		{
		"investmentType": 1,
		"amount": 2222,
		"createdAt": 1632987337,
		"id": "311142371136897216",
		},
		{
		"investmentType": 1
		"amount": 200,
		"createdAt": 1632987329,
		"id": "311142363330248896",
		},
		{
		"investmentType": 1,
		"amount": 100,
		"createdAt": 1632987316,
		"id": "311142348845220032",
		},
		{
		"investmentType": 0,
		"amount": 900
		"createdAt": 1632987306,
		"id": "311142338562883784",
		}
]

Now when I query only size of 2 without after I got the first two items of the code above.

[
		{
		"investmentType": 1,
		"amount": 2222,
		"createdAt": 1632987337,
		"id": "311142371136897216",
		},
		{
		"investmentType": 1,
		"amount": 200,
		"createdAt": 1632987329,
		"id": *"311142363330248896"*,
		}
]

Then I want to load more entries with this code. The bold lastRef is the id of the last already fetched doc (311142363330248896). But I am getting the same results as in the initial query without after (above). But I want to return results which are after that (last two in the one query of 4).

    q.Map(
      q.Paginate(
        q.Match(
          q.Index('reverse_entries_sort_user_crated'),
          '310621334632661191'
        ),
        {
          size: 2,
          after: [q.Ref(q.Collection('entries'), **lastRef**)],
        }
      ),
      q.Lambda(['data.cratedAt', 'ref'], q.Get(q.Var('ref')))
    )
  )

Hi @Radim_Hofer ,

Thank you for that information.

The after cursor is inclusive. So if you are passing lastRef as the id of the last fetched document to after, the result will include that document and the next one.

I can see expected behavior with Paginate

q.Paginate(q.Documents(q.Collection('People')))

{
  data: [
    Ref(Collection("People"), "311107731525206086"),
    Ref(Collection("People"), "311107731525207110"),
    Ref(Collection("People"), "311107731525208134"),
    Ref(Collection("People"), "311107731525209158"),
    Ref(Collection("People"), "311107731525210182"),
    Ref(Collection("People"), "311107731525211206"),
    Ref(Collection("People"), "311107731525212230")
  ]
}

With {size=2}

q.Map(
  q.Paginate(q.Documents(q.Collection('People')),{size:2}),
  q.Lambda('ref',q.Get(q.Var('ref')))
  )

{
  **after: [Ref(Collection("People"), "311107731525208134")]**,
  data: [
    {
      ref: Ref(Collection("People"), "311107731525206086"),
      ts: 1632954303230000,
      data: {
        first: "Alan",
        last: "Perlis",
        degrees: ["BA", "MA", "PhD"],
        letter: "A"
      }
    },
    {
      ref: Ref(Collection("People"), "311107731525207110"),
      ts: 1632954303230000,
      data: {
        first: "Alan",
        last: "Turing",
        degrees: ["BA", "MA", "MS", "PhD"],
        letter: "B"
      }
    }
  ]
}


When I next query with after, I get the next two documents

q.Map(
  q.Paginate(q.Documents(q.Collection('People')),{size:2, after:[q.Ref(q.Collection("People"),"311107731525208134")]}),
  q.Lambda('ref',q.Get(q.Var('ref')))
  )

{
  before: [Ref(Collection("People"), "311107731525208134")],
  after: [Ref(Collection("People"), "311107731525210182")],
  data: [
    {
      ref: Ref(Collection("People"), "311107731525208134"),
      ts: 1632954303230000,
      data: {
        first: "Grace",
        last: "Hopper",
        degrees: ["BA", "MA", "PhD"],
        letter: "C"
      }
    },
    {
      ref: Ref(Collection("People"), "311107731525209158"),
      ts: 1632954303230000,
      data: {
        first: "Leslie",
        last: "Lamport",
        degrees: ["BS", "MA", "PhD"]
      }
    }
  ]
}

It is not clear how the variable lastRef is getting populated. You may want to verify if that is carrying the right id value.