Need help building query for NextJS and prismic/client

Hi there,

I've been building out my portfolio and run into one issue trying to build a query for the homepage. The docs don't have a clear example that I can follow, or I've missed it.

I'm trying to query all my projects but I want to filter them by a boolean value (featured) and order them in ascending order but a numeric field (order).

I have this in my query:

const projects = await client.getAllByType("projects", {
    orderings: {
      field: "projects.order",
      direction: "asc",
    },
  });

What I'm stuck on is where the filter (predicates) should go in the params and does the order of the params matter?

Hey Andy,

I'm not 100% sure this will work, but hopefully it will help

import { predicate } from "@prismicio/client"

  const response = await client.get({
    predicates: [
      predicate.at("my.projects.featured", true),
      predicate.at("document.type", "projects"),
    ],
    orderings: [{ field: "my.projects.order", direction: "asc" }],
  })
  const projects = response.results
2 Likes

Thanks, I've given that a try and I think what is tripping me up is how I identify the projects from my JSON object ie. what you have called "my.projects".

I can't work out what the client will return from the API to then select my field.

I managed to work it out, thanks for the help.

This worked...

 const projects = await client.get({
    predicates: [
      predicate.at("my.projects.featured", true),
      predicate.at("document.type", "projects"),
    ],
    orderings: {
      field: "my.projects.order",
      direction: "asc",
    },
  });

1 Like

Hi Andy,

I think this is exactly my suggestion.

Glad that you got it working!

1 Like

@kris yes it was but initially I struggled to follow along and get it working. I hadn't read the docs properly about using "my" and that's what was confusing me.