Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Error: Unable to resolve image URL from source (undefined)" -> [slug].js: image[index] == undefined #99

Open
torsten92 opened this issue Mar 6, 2023 · 3 comments

Comments

@torsten92
Copy link

youtube comment:
Hi @javascript Mastery. Am getting an error at around 1:28:00. "Error: Unable to resolve image URL from source (undefined)". The product Image cannot be rendered. and yet it works so fine in the project. I realized someone had the same issue and raised in on stack overflow, though there was no reply.

reply:
The issue for me was, that "index" in image[index] is not reset.
"index" is set by onMouseEnter={() => setIndex(i).
When I load a new Product from the "maylike-products-wrapper", which has not as many images as the actual one, then image[index] is undefined.

I am just starting with Web-development, so not sure if this is good practice here:
urlFor(image && ((image[index] == undefined) ? image[0] : (image[index])))

instead of:
urlFor(image && image[index])

For debugging I have followed these instructions: https://youtu.be/q3WHdoz2KmE

torsten92 pushed a commit to torsten92/ecommerce that referenced this issue Mar 6, 2023
error debug:
 - catch undefined image[index]: urlFor(image && ((image[index] == undefined) ? image[0] : (image[index])))
 - adrianhajdin/ecommerce_sanity_stripe#99
 - https://youtu.be/q3WHdoz2KmE

tutorial: https://youtu.be/4mOkFXyxfsU?t=4585
@SoumyaSubhrajit
Copy link

hi , @topengl I may like to suggest 2 way of this issue,

1 ) The image are loading in the sanity db right, then you fetch the image by the url ,
in the client.js this pieces of code -> export const urlFor = (source) => builder.image(source); , now check the url fetching right or it gave some error, by
doing maual checks by await and fetch(), click this url some tutorial reference 👉 []
(https://www.youtube.com/watch?v=cuEtnrL9-H0&pp=ygULYXdhaXQgZmV0Y2g%3D).

  1. In the Product.jsx you do the image loading process remember, <img src={urlFor(image && image[0])} width={250} height={250} className="product-image" alt="" />
    this one is load and resizes the image and make it a container so that I can fetch the
    image and load it outside, then in the [slug.js] , we render out the image for the
    image by small-image-container, right then we iterate the hole image section and
    each of the image have urlFor(item) that gave the image address , and then the
    mouse point to certain image it will set the index value. and we see the change have
    been made in the slug page.
  2. see the code may be it will help you, <div className="small-images-container"> {image?.map((item, i) => ( <img // IF YOU CHANGE THE VALUE OF 'i' THEN THE IMAGE WILL CHANGE ACCORDINGLY.. key={i} src={urlFor(item)} className={i === index ? 'small-image selected-image' : 'small-image'} // THIS IS THE EVENT WHEN THE MOUSEPOINT THE REALTEED OBJECT onMouseEnter={() => setIndex(i)} /> ))}

if you have any further error, or any doubt my happy to help you with it.

@Madzeedy
Copy link

Madzeedy commented May 1, 2023

Hey topengl check on the first lines where you're declaring your class ProductDetails, you might be missing curly braces parameters. Keep it up Buddy

@khalilpyh
Copy link

My code is like this
<img src={urlFor(image && image.length > 1 ? image[index] : image[0])} className="product-detail-image" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants