Hello Priyanka,
I'm sending you the repo via DM!
This is my index (where there is my slices)
import { SliceZone} from '@prismicio/react'
import { createClient } from '@/prismicio'
import { components } from '@/slices'
import Head from "next/head";
import { Layout } from "../components/Layout";
export default function Page({ page }) {
return (
<Layout>
<Head>
<title>{page.data.title}</title>
</Head>
<main className='home-container'>
<SliceZone slices={page.data.slices} components={components} />
</main>
</Layout>
);
}
export async function getStaticProps({ previewData }) {
const client = createClient ({previewData})
const page = await client.getSingle('homepage');
return {
props: {
page
}
}
}
The one slice where there is the content relationship field I want to retrieve:
{
"id": "home_one_image",
"type": "SharedSlice",
"name": "Home1Image",
"description": "Home1Image",
"variations": [
{
"id": "default",
"name": "Default",
"docURL": "...",
"version": "initial",
"description": "Default",
"imageUrl": "",
"primary": {
"project": {
"type": "Link",
"config": { "label": "Project", "select": "document" }
},
"short_description": {
"type": "StructuredText",
"config": {
"label": "Short description",
"placeholder": "",
"allowTargetBlank": true,
"single": "paragraph,preformatted,heading1,heading2,heading3,heading4,heading5,heading6,strong,em,hyperlink,image,embed,list-item,o-list-item,rtl"
}
},
"image_1": {
"type": "Image",
"config": { "label": "Image 1", "constraint": {}, "thumbnails": [] }
}
},
"items": {}
},
{
"id": "withOneVideo",
"name": "with One video",
"docURL": "...",
"version": "initial",
"description": "Default",
"imageUrl": "",
"primary": {
"project": {
"type": "Link",
"config": { "label": "Project", "select": "document" }
},
"short_description": {
"type": "StructuredText",
"config": {
"label": "Short description",
"placeholder": "",
"allowTargetBlank": true,
"single": "paragraph,preformatted,heading1,heading2,heading3,heading4,heading5,heading6,strong,em,hyperlink,image,embed,list-item,o-list-item,rtl"
}
},
"video_id": {
"type": "Text",
"config": { "label": "Video ID", "placeholder": "" }
}
},
"items": {}
}
]
}
and this is the index.js from the slice :
import * as prismicH from "@prismicio/helpers";
import { PrismicNextImage } from "@prismicio/next";
import { createClient } from '@/prismicio'
import { GraphQLClient, gql } from "graphql-request";
import { PrismicLink, PrismicText, SliceZone } from "@prismicio/react";
import { PrismicRichText } from "@prismicio/react"
/**
* @typedef {import("@prismicio/client").Content.Home1ImageSlice} Home1ImageSlice
* @typedef {import("@prismicio/react").SliceComponentProps<Home1ImageSlice>} Home1ImageProps
* @param {Home1ImageProps}
*/
const Home1Image = ({ slice }) => {
const image1 = slice.primary.image_1;
const sliceVariation = slice.variation;
const projectslug = slice.primary.project.uid;
const client = createClient();
const project_ = client.getByUID("project", projectslug);
/// this works & returns a promise pending like in screenshot
console.log(project_);
/// this works
console.log(projectslug);
/// this returns the error "TypeError: Cannot read properties of undefined (reading 'project_name')"
console.log(project_.data.project_name)
return (
<section
className={`home-layout home-layout-1 ${sliceVariation}`}
data-slice-type={slice.slice_type}
data-slice-variation={slice.variation}
>
<div className="project-title">
(variation: {slice.variation}) <br></br>
{projectslug} (the project slug is showing)
<PrismicRichText field={slice.primary.short_description} />
</div>
<div className="images-container">
{slice.variation === 'withOneVideo' ? (
<video autoPlay muted playsInline poster="http://localhost:3000/videcover.jpg">
<source src="https://player.vimeo.com/video/${slice.primary.video_id}" type="video/mp4" />
Your browser does not support the video tag.
</video>
) : (
<>
{prismicH.isFilled.image(image1) && (
<div className="image-container">
<PrismicNextImage
field={image1}
className="object-cover"
alt=""
/>
</div>
)}
</>
)}
</div>
</section>
);
};
export default Home1Image;
and inside my repeatable project type, I have a field called "project_name" (data.project_name).
I need to display the project name and get the url to link to the project.
Let me know if that's sufficient!