Query UID of content relationship within repeatable group

Hello,

I have a sidebar containing product categories and a grid of products. When the user selects a particular category, I need to load all of the products in that category.

In my Prismic repository, I have Category and Product types. Product contains a repeatable group of content relationships which link to one or more categories (i.e. 'Featured' and 'Travel'). These categories have a UID (i.e. 'featured') I am therefore trying to query based on this repeatable group as such:

const products = await this.$prismic.api.query(
    this.$prismic.predicates.at("my.product.categories.category", category.id) // category.id = 'featured'
)
console.log(products.results)

This seems to run without any errors, however products.results always contains 0 items.

Is there something I'm doing wrong here? Should it be possible to use this strategy, or is there a better way?

For reference, I'm using Nuxt.js.

Thanks!

I'm thinking of just adding a products repeatable group to each Category type. That way I can just query the Category UID to get the product UIDs.

I still feel like there must be a better solution however, so any ideas would be much appreciated.

Hey @andreww500,

Welcome to the Prismic community, and thanks for posting this question :slight_smile: :handshake:

This had me puzzled for a moment. Your query looks totally fine. But I think I see the issue. When you query by a Content Relationship, you need to pass the Document ID, not the UID.

For each result, you have some front matter that looks like this:

"id": "Xz-ajBAAACQA_P9h",
"uid": "config",
"url": null,
"type": "config",
"href": "https://sam-onbo...",
"tags": [],
"first_publication_date": "2020-08-21T09:57:36+0000",
"last_publication_date": "2020-11-17T14:03:11+0000",
"slugs": [
"config-"
],
"linked_documents": [],
"lang": "en-us",
"alternate_languages": [],

It looks like you're using the UID, and calling it ID, but each document already has a default ID property, which is a unique, random string. That is the property you need in order to query by Content Relationship.

Please let me know if that makes sense! And, in any case, let me know if you have more questions.

Best,
Sam

Thanks for the response @samlittlefair!

For now I've gone with the second method I mentioned which is working great (albeit perhaps not as efficient). I'll revisit your suggestion once I come to refactor it - I was definitely using the UID, so I'd imagine that switching to ID will indeed resolve the issue.

Great. Let me know if you have any more questions.

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