Slices and linked document fields

Hi there,

I am trying to create in a separate function all the slices available on my site to retrieve them when needed. So far so good, but when trying to get the fields of the linked document fields, I get a [[PromiseResult]] and can't get the data. I am learning Prismic for a couple days only and so I am probably doing something wrong here, but since I can log the values correctly, I don't get what is wrong. I can console.log them but can't access them. Here is the part of the function I am talking about :


const getSlices = page => {
return page.data.body.map(slice => {
switch(slice.slice_type) {
case 'concept_clef':
return {
conceptClef: {
titre: slice.primary.titre_section,
intro: slice.primary.intro_section
}
}
case 'list_of_articles':
return {
listArticles: {
titre: slice.primary.titre_section,
intro: slice.primary.intro_section,
articles: slice.items.map(async item => {
return await Client.getByID(item.articles_to_link.id)
})
}
}
})
}

Thanks for your help !

Hello Stephanie,

Thanks for posting this question to us.

You need to use fetchLinks in your code. fetchLinks fetches a specific content field from a linked document. Find more detail here . This is in javascript but you can find documentation in the different frameworks as well.

You need to use fetchLinks in your code. In your fetchLinks array, the value should be:

  • First, the API ID of the custom type referenced in your content relationship field.
  • Then, the API ID of the field that you want to retrieve.

Let me know if it doesn't work for you, we can work together to solve this.

Thanks,
Priyanka

Hi and thanks Priyanka for your kind return ! I already went on the link you shared with me, but the fields I am accessing are in a slice zone and the page you shared mentions that "It is not possible to retrieve the following content field types: Any field in a Group or Slice".

So I found the workaround below in order to access the data anyway to use them in my template and it works great but I wonder if it is ok for Prismic though.

articles: slice.items.map(item => {
let results =
Client.getByID(item.articles_to_link.id).then(res => results.push(res))
return results
})

Thanks !

@Priyanka, I might have not been precise in my answer but if there is a way to retrieve content field types in a slice group, it would be fantastic if you could share the way to get all this working. To be more precise on what I am trying to get : I am linking 4 articles on the homepage and the api only retrieve the id of the article, where I would need the title, excerpt, image...
Thanks !

To retrieve linked documents from Slices, you need to use graphQuery. It's an advanced version of fetchLinks.
Please follow this doc: API reference for GraphQuery (fetchLinks v2) - Prismic

Let me know if you have any doubt.

Thanks,

Priyanka

That made the trick :slight_smile: Thanks Priyankaa for your help !

Hello Stephanie,

I am glad that It works for you. Don't hesitate to reach out to us with any questions.

Thanks,
Priyanka

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

There is no way to retrieve linked documents from slices using fetchlinks without the graphQuery ? THis is mandatory ?

Hello Andreas,

fetchLinks can not retrieve the following fields:

  • Embed
  • GeoPoint
  • Link
  • Link to Media
  • Rich Text (anything other than the first element)
  • Any field in a Group or Slice

So you need to use graphQuery to fetch content from Slices. Are you getting any trouble in using graphQuery? I'll be happy to assist you.

Thanks,
Priyanka

@Priyanka I'm struggling to resolve some data with either fetchLinks or graphQuery.
I have a content type landingPage which has the key text field slug. In that content type I use a slice called TextBlock. This slice has a rich text field. In that field I've added a link to a different landingPage document.
I'm unable to resolve the slug field for the linked landingPage inside the slice's rich text field. Could you explain how it could be done?

@Priyanka Sorry, I was just stupid. I used getFirst and put the fetchLink and lang in a second parameter, that doesn't exist. it's working fine!