👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Issue with implementing pagination for entries in an array in a Slack thread.

2 replies
Last updated: Aug 19, 2023
I’m trying to implement pagination for entries that are in an array, but I can’t seem to get any results.

*[_type == 'market' && siteDomain == "<http://www.website.se|www.website.se>"][0] {
  'tripCollections': tripCollections[@->nameSv in ["Spain"]] -> {
    'name': @.['nameSv'],
    'trips': trips[_id > "trip-17058401-3baa-40cf-8fc9-11869112b117"] | order(_id) [0...3] -> {
      _id,
      recipe,
      'images': images[]->image.asset->url,
      'headline': contentSv->headline,
      'salesPitch': contentSv->salesPitch,
      'slug': contentSv->slug
    }
  }
}

trips[_id > "trip-17058401-3baa-40cf-8fc9-11869112b117"] | order(_id) [0...3]
contains the _id of the very first item in the array of elements, then I try to fetch 0 to 3.
But for some reason I just get an empty result.
Is there anything obvious that I’m doing wrong?

If it wasn’t clear already,
trips
is an array. Also not sure if it matters but the order of the elements are important to me, I wish to preserve them the way they are in listed in the CMS.
Also important to note that the array is an array of references
Aug 19, 2023, 12:18 PM
OK, I figured out what the problem was. The problem was that I had to do pagination based on the
_ref
property instead of
_id
, because
_id
only appears after the dereference.
Aug 19, 2023, 1:39 PM
Here’s the updated query if anyone’s interested.

*[_type == 'market' && siteDomain == "<http://www.website.se|www.website.se>"][0] {
  'tripCollections': tripCollections[@->nameSv in ["Spain"]] -> {
    'name': @.['nameSv'],
    'trips': trips[_ref > "trip-17058401-3baa-40cf-8fc9-11869112b117"] | order(_ref) [0...3] -> {
      recipe,
      'images': images[]->image.asset->url,
      'headline': contentSv->headline,
      'salesPitch': contentSv->salesPitch,
      'slug': contentSv->slug
    }
  }
}
Aug 19, 2023, 1:41 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?