试图解决错误“ urlforimage.ts:43未接收错误:无法从源(Undefined)解析图像URL”。使用理智并反应

发布于 2025-02-13 18:58:27 字数 2525 浏览 1 评论 0原文

我正在使用与JavaScript的理智并进行反应遇到错误。该应用程序是一个个人投资组合网站,我已经成功地将数据从SANITY中获取了其他3个部分。现在,我正在研究推荐部分,我正在尝试拉2张当前在Sanity Studio中加载的图像(JPEG):Teastimonials> Imgurl 在我的返回声明中,我正在检查推荐时间的长度,以便我只有至少一个出现时才能获取。在图像标签中,我正在使用src = {urlfor(testimonials [currentIndex] .imgurl)}

代码块

return (
<>
  {testimonials.length && (
    <>
      <div className="app__testimonial-item app__flex">
        <img src={urlFor(testimonials[currentIndex].imgurl)} alt="testimonial" />            
      </div>
    </>
  )}
</>

:);


 1. urlFor is the function used to fetch images off Sanity, as per their documentation.
 2. currentIndex is the useState inside of the Testimonial function, which acts as the container component for this section of the website. It will keep track of the state of the image being displayed and will cycle through others with a clickable action (code not present here)


**Question:** I am getting an error that is caused by the img tag and cannot find anything online about a fix. This [thread][1] does not fix my issue. 


**ERROR MESSAGE**:
react-dom.development.js:20085 The above error occurred in the <img> component:
    at img
    at div
    at Testimonial (http://localhost:3000/main.20b2c7aaa42080546b80.hot-update.js:37:90)
    at div
    at VisualElementHandler (http://localhost:3000/static/js/bundle.js:119980:38)
    at MotionComponent (http://localhost:3000/static/js/bundle.js:119880:20)
    at HOC
    at div
    at div
    at HOC
    at div
    at App
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.


Error Message

Uncaught Error: Unable to resolve image URL from source (undefined)
    at urlForImage (urlForImage.ts:43:1)
    at ImageUrlBuilder.url (builder.ts:229:1)
    at ImageUrlBuilder.toString (builder.ts:234:1)
    at setValueForProperty (react-dom.development.js:672:1)
    at setInitialDOMProperties (react-dom.development.js:8931:1)
    at setInitialProperties (react-dom.development.js:9135:1)
    at finalizeInitialChildren (react-dom.development.js:10201:1)
    at completeWork (react-dom.development.js:19470:1)
    at completeUnitOfWork (react-dom.development.js:22815:1)
    at performUnitOfWork (react-dom.development.js:22787:1)


  [1]: https://stackoverflow.com/questions/69031649/error-unable-to-resolve-image-url-from-source-null

I am running into an error using Sanity with Javascript and React. The app is a personal portfolio site and I have successfully pulled data from Sanity for 3 other sections. Now that I am working on the Testimonials section, I am trying to pull 2 images (jpegs) that currently loaded in Sanity Studio: testimonials > imgurl
In my return statement, I am checking for length of testimonials so that I only fetch if at least one is present. Inside the image tag, I am using src={urlFor(testimonials[currentIndex].imgurl)}

Code Block:

return (
<>
  {testimonials.length && (
    <>
      <div className="app__testimonial-item app__flex">
        <img src={urlFor(testimonials[currentIndex].imgurl)} alt="testimonial" />            
      </div>
    </>
  )}
</>

);


 1. urlFor is the function used to fetch images off Sanity, as per their documentation.
 2. currentIndex is the useState inside of the Testimonial function, which acts as the container component for this section of the website. It will keep track of the state of the image being displayed and will cycle through others with a clickable action (code not present here)


**Question:** I am getting an error that is caused by the img tag and cannot find anything online about a fix. This [thread][1] does not fix my issue. 


**ERROR MESSAGE**:
react-dom.development.js:20085 The above error occurred in the <img> component:
    at img
    at div
    at Testimonial (http://localhost:3000/main.20b2c7aaa42080546b80.hot-update.js:37:90)
    at div
    at VisualElementHandler (http://localhost:3000/static/js/bundle.js:119980:38)
    at MotionComponent (http://localhost:3000/static/js/bundle.js:119880:20)
    at HOC
    at div
    at div
    at HOC
    at div
    at App
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.


Error Message

Uncaught Error: Unable to resolve image URL from source (undefined)
    at urlForImage (urlForImage.ts:43:1)
    at ImageUrlBuilder.url (builder.ts:229:1)
    at ImageUrlBuilder.toString (builder.ts:234:1)
    at setValueForProperty (react-dom.development.js:672:1)
    at setInitialDOMProperties (react-dom.development.js:8931:1)
    at setInitialProperties (react-dom.development.js:9135:1)
    at finalizeInitialChildren (react-dom.development.js:10201:1)
    at completeWork (react-dom.development.js:19470:1)
    at completeUnitOfWork (react-dom.development.js:22815:1)
    at performUnitOfWork (react-dom.development.js:22787:1)


  [1]: https://stackoverflow.com/questions/69031649/error-unable-to-resolve-image-url-from-source-null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2025-02-20 18:58:27

转到架构中的证明文件,然后更改本节:

    {
        name:'imgurl',
        title:'ImgUrl',
        type: 'image',
        options: {
          hotspot: true,
    },

Go to Your testimonial file in schema and change this section:

    {
        name:'imgurl',
        title:'ImgUrl',
        type: 'image',
        options: {
          hotspot: true,
    },
征﹌骨岁月お 2025-02-20 18:58:27

只需使用此问题 - 将为您工作,

{Data.image && (<img src={urlFor(Data.image.url()} alt="testimonial" />)}

而不是使用iMgurl或使用URL()。
我也不明白您为什么使用[CurrentIndex]。但是,无论如何..

Just use this instead - will work for you

{Data.image && (<img src={urlFor(Data.image.url()} alt="testimonial" />)}

Instead of using imgurl or use url().
Also I don't understand why you're using [currentIndex]. but ya anyway..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文