Different params options with Next.js query

I'm trying to do the same thing Next.js, took a guess at it, but after doesn't seem to be doing much - im getting the same project back for next as I am for the main page.

  const nextProject = await client.getAllByType("project", {
    limit: 1,
    after: `${params.uid}`,
    orderings: [
      {
        field: "project.data.completion_date",
        direction: "desc",
      },
    ],
  });

Hello @constructivearchitec

There are three things to correct with the code:

  1. The after param tells the API to return all documents after a given document, specified by its ID, not by UID.
  2. Prismic doesn't have limit params. You need to set the page property to define what page of the API results to return, starting with and defaulting to 1.
  3. Specify the orderings params field with the path my.[custom-type].[field].

So, with the above params option, your query should look like this:

const nextProject = await client.getAllByType("project", {
    page: 1,
    after: `${project.id}`,
    orderings: [
      {
        field: "my.project.completion_date",
        direction: "desc",
      },
    ],
  });

Give this a try, and let me know.

Thanks,
Priyanka

Thanks for this... It worked with one small change, since completion_date is a custom field its at my.project.data.completion_date.

const apiNextProject = await client.getAllByType("project", {
    page: 1,
    after: `${project.id}`,
    orderings: [
      {
        field: "my.project.data.completion_date",
        direction: "desc",
      },
    ],
  });

But the other problem I ran into is that on the last project, nothing is returned. I guess I could also come up with a function to get the first project to loop back to, but for now (since the site will likely never have more than 6-10 projects), im just passing all the projects and getting next by index.

  const nextProject =
    projects[projects.findIndex((x) => x.id === project.id) + 1] || projects[0];