Watch a live product demo 👀 See how Sanity powers richer commerce experiences

How to Fetch Image From Client - Blank Image in Response

23 replies
Last updated: May 16, 2022
Hey guys how to fetch image from client. I am fetching image but it returns blank image
May 16, 2022, 6:03 PM
{

name: 'image',

title: 'Image',

type: 'array',

of: [{ type: 'image' }],

options: {

hotspot: true,

}

},
May 16, 2022, 6:04 PM
This is the schema
May 16, 2022, 6:04 PM
 I am giving {fetchedData.image} as src of img
May 16, 2022, 6:06 PM
Images exist as references to an image asset in Sanity, so you need to expand the reference in order to get the url. There's a good introduction to presenting images here .
May 16, 2022, 6:08 PM
user M
I used the url builder. What now? how to get image src
May 16, 2022, 6:31 PM
What does your code look like?
May 16, 2022, 6:33 PM
import React, { useEffect, useState, useLayoutEffect } from 'react';

import { Link } from "react-router-dom";

import Header from "../components/Header";

import "./Home.css";

import {Carousel, Card, Image} from 'antd';

import {client} from "../lib/client";



const Home = () => {

const [products,setProducts]= useState([]);

const [bannerData,setBannerData]= useState([])


useEffect(() => {
`client.fetch(
*[_type == "featured"]{

name,

genre,

image

}
).then((data)=>setProducts(data))`
}, [])


useLayoutEffect(() => {
`client.fetch(
*[_type == "banner"]{

image

}
).then((data)=>setBannerData(data))`
}, [])


return(

<>

<div className="container">

<Header/>

<Carousel autoplay className="carousel">

{bannerData?.map((data) => {

return <img src={urlFor(data.image).url()} className="carousel-img" alt="carousel"></img>;

})}

</Carousel>

<div className="cards">

{products.map((product)=>{

return(

<Card className="card" key={product._id}>

<Link to="/categories" state={product.genre} className="link" style={{textDecoration:"none"}}>

<img src={urlFor(product.image).url()} alt={product.name} className="card-content"/>

<h4>{product.name}</h4>

</Link>

</Card>

)

})}

</div>

</div>

</>

)



}

export default Home;
May 16, 2022, 6:34 PM
I have url builder function in client
May 16, 2022, 6:34 PM
but how to use it here
May 16, 2022, 6:35 PM
import sanityClient from '@sanity/client';

import imageUrlBuilder from '@sanity/image-url'


export const client = sanityClient({

projectId:'uykjgyz0',

dataset:'production',

apiVersion:'2022-03-10',

useCdn:true,

token:process.env.SANITY_TOKEN

});


const builder= imageUrlBuilder(client)

export function urlFor(source) {

return builder.image(source)

}
May 16, 2022, 6:37 PM
Client.js
May 16, 2022, 6:37 PM
Please help anyone
May 16, 2022, 6:54 PM
You'll need to import your
urlFor
function wherever you want to use it, then pass your image asset to the function. It looks like you're doing that with your product image but not your banner data.
May 16, 2022, 6:59 PM
user M
I am doing that in bannerData too and it says Uncaught (in promise) Error: Unable to resolve image URL from source ([{"_key":"c86fb91a8ad3","_type":"image","asset":{"_ref":"image-48f1b8fa7db78f55843dcd58547080123dc36adb-192x293-webp","_type":"reference"}}])
May 16, 2022, 7:00 PM
Can you try passing in the asset instead of the entire image?
May 16, 2022, 7:01 PM
What does it mean to pass the asset?image.asset?
May 16, 2022, 7:02 PM
Yes
May 16, 2022, 7:03 PM
Now it says Uncaught (in promise) Error: Unable to resolve image URL from source (undefined) at urlForImage (urlForImage.ts
:43:1) at ImageUrlBuilder.url (builder.ts
:229:1) at Home.js
:42:1 at Array.map (<anonymous>)
at Home (Home.js
:37:1) at renderWithHooks (react-dom.development.js
:14985:1) at updateFunctionComponent (react-dom.development.js
:17356:1) at beginWork (react-dom.development.js
:19063:1) at HTMLUnknownElement.callCallback (react-dom.development.js
:3945:1) at Object.invokeGuardedCallbackDev (react-dom.development.js
:3994:1)
May 16, 2022, 7:03 PM
Strangely the docs says passing the image is enough
May 16, 2022, 7:05 PM
Now you can use this handy builder-syntax to create your URLs:
<img src={urlFor(person.image).width(200).invert().flipHorizontal().url()} />

May 16, 2022, 7:06 PM
This is what documentation says
May 16, 2022, 7:06 PM
Now i tried this &lt;img src={urlFor(`<https://cdn.sanity.io/images/uykjgyz0/production/${data.image.asset._ref.slice(6)}>`).url()}
May 16, 2022, 7:26 PM
Still doesn't work
May 16, 2022, 7:27 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern content platform that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing