Next.js Errors when trying to map Slugs to Articles
slug.current == $slugpart filters the results from
_type == "post"to only return posts where the slug matches the
$slugvariable supplied to the query
postdocuments which have a
slugfield with a value that is equal to the
slugargument given to the query.
[0]part simply gets the first (hopefully only) one of these results to eliminate the need to traverse to the first array item in your code
*[_type == "nouvelles"&& slug.current == $slug][0]{_id,
title,
publishedAt,
"slug": slug.current,
"categories": category[]->{title, slug},"imageUrl": mainImage.asset->url,
body,
}
The error would go away on nouvelles.js when I remove the highlighted expression in my original question, but then I wouldn't get the proper content when I click into an article ( [slug].js ).
Will I need a separate query for this to work properly, or am I doing something wrong here?
$slugparameter, but you do not provide any. So either it doesn’t need to check for the slug and that condition needs to be removed, or you need to pass a slug parameter. 🙂
getStaticProps?
getStaticProps.
*[_type == "nouvelles"][]{
_id,
title,
publishedAt,
"slug": slug.current,
"categories": category[]->{title, slug},
"imageUrl": mainImage.asset->url,
body,
} *[_type == "nouvelles" && slug.current == $slug][0]{
_id,
title,
publishedAt,
"slug": slug.current,
"categories": category[]->{title, slug},
"imageUrl": mainImage.asset->url,
body,
}getStaticPropsmethod might fail on that page because its looking for
params.slugwhich may not exist if you're not on a route that specifically uses
slugas a named paramater (I'm assuming this is NextJS?)
*[_type == "nouvelles"]{...Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.