Prismic.Predicates at month and year not working together to filter concise posts

Hey! This is my first time posting here.

I am following the example below:

client.query([
    Prismic.Predicates.month('my.blog_post.release_date', 'May'),
    Prismic.Predicates.year('my.blog_post.release_date', 2016)
]).then(res => {
    // res is the response object, res.results holds the documents
})

In order to implement an archive for my client's blog. I am finding success when I am using the Prismic.Predicates.month OR Prismic.Predicates.year.... but not when they are being used together to return a result. I am receiving the following error:

image

My code is as follows. Note that the variable 'year' is stored in my state from another listener, so it is not returning 'null', it is returning whatever date I select on my dropdown.

const queryByDate = (e) => {
  let month = e.target.value;
  const fetchDates = async (month, year) => {
    const response = await Client.query(
      Prismic.Predicates.month('my.blog.post_date', month),
      Prismic.Predicates.year('my.blog.post_date', year)
    );
    if (response) {
      setDocData(response.results);
      console.log("response", response.results);
    }
  }
  fetchDates(month, year);
}

Any help would be appreciated! Like I said the month OR year will work in returning posts, but not both together, any ideas?

Thanks!

Hello, @justsoup

Welcome to the Prismic community, and thanks for reaching out to us.

I find in your code that you need to combine more than one predicates to refine your query. Put all your predicates into a comma-separated array like the following example:

const fetchDates = async (month, year) => {
    const response = await Client.query([
      Prismic.Predicates.month('my.blog.post_date', month),
      Prismic.Predicates.year('my.blog.post_date', year)
    ]);
    if (response) {
      setDocData(response.results);
      console.log("response", response.results);
    }
  }

Give this a try and let me know.

Thanks,

Priyanka

Hi Priyanka!

Thank you for the help! I realize now that the problem in my code was this:

 const response = await Client.query(
  Prismic.Predicates.month('my.blog.post_date', month),
  Prismic.Predicates.year('my.blog.post_date', year)
);

The Prismic predicates weren't in an array like below:

const response = await Client.query([
  Prismic.Predicates.month('my.blog.post_date', month),
  Prismic.Predicates.year('my.blog.post_date', year)
]);

Thanks again!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.