0

I was fetching my images in cart page. I wanted just 1 image so my code is below if you guys help me out my pleasure.

import { urlFor } from "@/sanity/lib/image";
import createImageUrlBuilder from "@sanity/image-url";
import { SanityImageSource } from "@sanity/image-url/lib/types/types";
import { dataset, projectId } from "../env";
// Initialize the image URL builder
const builder = createImageUrlBuilder({ projectId, dataset });
// Function to generate image URLs from Sanity images
export const urlFor = (source: SanityImageSource) => {
 return builder.image(source);
};

And below is my cart page while I am fetching images

if (!product || !product.images || !product.images.length) return null;
return (
 <div
 key={index}
 className="h-auto flex items-start mb-5 space-x-4 p-4 dark:bg-slate-900 border rounded-xl shadow-sm"
 >
 <div className="relative h-20 w-20 rounded-lg overflow-hidden">
 <Image
 src={urlFor(product?.images[0]?.asset?._ref).url() || "/noimage.png"}
 alt={"Product Image"}
 fill
 className="object-cover"
 loading="lazy"
 />
 </div>
 </div>
);
Maulik Bhalani
7922 gold badges7 silver badges24 bronze badges
asked Mar 20, 2025 at 4:35
2
  • It'd be helpful if you shared 1) the error or issue, 2) the value of product.images[0].asset._ref, and 3) what Image is. Without any of that, it's really hard to give you any kind of direction. Commented Mar 21, 2025 at 3:19
  • 1: I see a issue undefined. 2) vlaue of product.images[0].asset.._ref is shown unrefined. 3) image is a product image Commented Mar 21, 2025 at 14:38

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.