Render data using fetchLinks

I'm trying to render data from a linked document in my Next.js project. From what I understand, I need to use FetchLinks but am unable to work out how to render the queried data in my template.

My Prismic data is below, where I am trying to pull in data from a document type called Person as the documents Author:

I think I've set up the function to reference the person data using fetchLinks but am unable to work out how to display the data in my template e.g. the person name

import React from 'react'

import DefaultLayout from 'layouts'
import { Header, SliceZone } from 'components'

import { queryRepeatableDocuments } from 'utils/queries'
import { RichText } from 'prismic-reactjs'

import { Client } from 'utils/prismicHelpers'

const Article = ({ doc, menu }) => {
  if (doc && doc.data) {
    return (
      <DefaultLayout>
        <div className="page">
          <Header menu={menu} />
          <div className="container my-12">
            {RichText.render(doc.data.title)}
            {RichText.render(doc.data.headline)}
          </div>
          <div className="container grid md:grid-cols-3">
            <div>{RichText.render(doc.data.author.name)}</div>
            <div className="md:col-span-2">{RichText.render(doc.data.lead_paragraph)}</div>
          </div>
          <SliceZone sliceZone={doc.data.body} />
        </div>
      </DefaultLayout>
    )
  }

  // Call the standard error page if the document was not found
  return null;
}


export async function getStaticProps({ params, preview = null, previewData = {} }) {
  const { ref } = previewData

  const client = Client()

  const doc = await client.getByUID('opinion', params.uid, {'fetchLinks': ['person.uid', 'person.name']}, ref ? { ref, } : null) || {}
  const menu = await client.getSingle('menu', ref ? { ref } : null) || {}
  
  return {
    props: {
      preview,
      menu,
      doc
    }
  }
};

export async function getStaticPaths() {
  const documents = await queryRepeatableDocuments((doc) => doc.type === 'opinion')
  return {
    paths: documents.map(doc => `/opinion/${doc.uid}`),
    fallback: true,
  }
}

export default Article

Any help would be greatly appreciated.

Hi @mike1,

Thanks for posting, and welcome to the Prismic community! From a first look at your code, I'm not sure what the issue is. Could you let me know your repo name, so I can try to recreate the issue? (Feel free to send it in a DM if you like.)

Sam

Hey @samlittlefair,

Thanks for the welcome!

My issue is around RichText.render(doc.data.author.name)} not rendering the name. I tried RichText.render(doc.data.person.name)} but that threw an error. I get a Rich text argument should be an Array. Received undefined warning when I use author.name, but no error.

I'll DM you the repo.

Cheers,
Mike

Hey @mike1,

Okay, looking more closely at this, in the screenshot of your API response, it looks like you're using GraphQL, but in the code snippet it looks like you're using the Rest API. If you're using the Rest API, fetchLinks should work fine, just as you have used it. But, if you're using GraphQl, you'll need to use deep fetching: Retrieve linked document content with the Prismic GraphQL API - Prismic

Let me know if that helps!

Sam

Hey @samlittlefair — the API response was just how I was looking at the data in API Browser — I'm not using GraphQL in this project.

Turns out I had to go one level deeper with my reference so instead of {RichText.render(doc.data.author.name)} I needed {RichText.render(doc.data.author.data.name)}

All sorted!

Great! Thanks for the update, @mike1 :slight_smile:

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