Trouble uploading images with sanity.io, resolved with alternate method

24 replies
Last updated: Aug 8, 2022
Hi there, I have implemented sanity.io in my project and I am having trouble trying tpo upload images in it. Specifically, I am using the imageBuilder in order to upload them but it looks like it's not working as when I am using the urlFor function in my component, it says that the data pulled it's undefined. When I have figure it out, I have tried to check if I was pulling the data in the right way and evntually I was doing it correctly as i could get the assetId and the type.
AI Update

It sounds like you're running into a common issue with image references in Sanity! The problem is likely that urlFor() is receiving an undefined value even though you can see the _type and asset reference when logging your data.

Here are the most common causes and solutions:

1. Passing the wrong part of the image object

When you query an image field from Sanity, you get an object with properties like _type, asset, hotspot, and crop. You need to pass the entire image object to urlFor(), not just the asset reference.

// ❌ Wrong - passing just the asset reference
urlFor(post.mainImage.asset)

// ✅ Correct - pass the entire image object
urlFor(post.mainImage)

2. Your GROQ query might not be fetching the image data correctly

Make sure your query includes the image field properly. Here's a good pattern:

// Basic query
const query = `*[_type == "post"][0] {
  title,
  mainImage
}`

// Or with expanded asset reference (optional but helpful for debugging)
const query = `*[_type == "post"][0] {
  title,
  mainImage {
    asset->,
    hotspot,
    crop
  }
}`

3. Check your urlFor setup

Make sure your urlFor helper is configured correctly with your project ID and dataset as shown in the @sanity/image-url documentation:

import imageUrlBuilder from '@sanity/image-url'
import { client } from './client' // your sanity client

const builder = imageUrlBuilder(client)

export function urlFor(source) {
  return builder.image(source)
}

4. Handle null/undefined values

Add a safety check in your component:

{post.mainImage && (
  <img src={urlFor(post.mainImage).width(800).url()} alt={post.title} />
)}

Debugging tip: Log the entire data structure you're receiving:

console.log('Full post data:', JSON.stringify(post, null, 2))
console.log('Image field:', post.mainImage)

Since you mentioned you can see the _type and assetId, you're definitely getting data back - you just need to make sure you're passing the complete image object (not drilling down to .asset) to the urlFor() function. The image type stores asset references along with metadata like hotspot and crop information, and the URL builder needs the full object to work properly.

We’re going to need to see some code. 🙂
HI
user F
, I will share my code. So briefly, the ResturantCard component is where I am using the urlFor() function. As you will see the imgUrl Prop is imported from the component FeaturedRow.js where I have imported my data from sanity using the GROQ function.
So I have stored sanity data in a state called restaurants and then in order to get all the key value I am mapping through resturants array stored in the state.
The issue is when I am trying to retrieve imgUrl in RestaurantCard.js using the sanity imageBuilder as it gives me an error saying that url is undefined
otherwise I will share sanity.js where I have imported @sanity/client and @sanity/image-url
Can you show me the error?
This is the error I get
This is how looks the image section in my sanity schema
ANd this is how looks my restaurant.js in the sanity/schemas folder when I have created the data
could you
console.log(imgUrl)
and try this:
restaurants?.map(resto => <RestaurantCard 
  key={resto._id}
  id={resto._id}
  title={resto.name}
  //etc.
  lat={resto.lat}/>)

HI
user J
I have console.log the imgUrl prop and this is what I get. It seems to me that this prop has been imported correctly and I get the image data correctly. So now I need to understand why the image/builder doesn't work with this data
Have you tried what I suggested before?
In addition you can also try to use
urlFor
outside of your return function:
const image= urlFor(imgUrl).width(400).height(300).url()
and then use it in your
<Image/>
component.
Yes
user J
I have tried what you have suggested before and it works as I get the data from sanity. When I console.log the imgUrl I have got asset and type, are those key value enough to be used in the image/builder?
Or better, are this the right keys value to use with image/builder?
yes they are!
const image= urlFor(imgUrl).width(400).height(300).url()
I have tried it and it gives me the same error unfortunately
Hi
user J
I have figured it out. I found a way to extract the asset_id and url, and I have basically add to Image component the source imgUrl.asset.url
And it worked.
I still need to figure out how image/builder works
😀😀
you got it to work?
Yes
user J
I’ve used a different way. When I have fetched the data from sanity in my project, I have pulled out from image{ asset -&gt; {_id, url}}. So then as source of the Image component I have used the source=imgUrl.asset.url and it worked
But I still don't know how to use the image/builder

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?