Trouble uploading images on website project, resolved with help from Slack community.

12 replies
Last updated: Sep 9, 2022
Hello everyone
I've got a little problem with uploading images by sanity on my website project. I think I did everything right but the pictures in the "work" section still don't render . i'll show you some screens with code and sanity desk.


import React, { useState, useEffect } from 'react';
import { AiFillEye, AiFillGithub } from 'react-icons/ai';
import { motion } from 'framer-motion';
import { AppWrap } from '../../wrapper';
import { urlFor, client } from '../../client';
import './Work.scss';

const Work = () => {

  const [activeFilter, setActiveFilter] = useState('All');
  const [animateCard, setAnimateCard] = useState({ y: 0, opacity: 1 });
  const [works, setWorks] = useState([]);
  const [filterWork, setFilterWork] = useState([]);

  useEffect(() => {
    const query = '*[_type == "works"]';

    client.fetch(query)
      .then((data) => {
        setWorks(data);
        setFilterWork(data);
      })
  }, [])
  

  const handleWorkFilter = (item) => {

  }

  return (
    <>
      <h2 className="head-text">My Creative <span>Portfolio </span>Section</h2>

      <div className="app__work-filter">
        {['UI/UX', 'Web App', 'Mobile App', 'React JS', 'All'].map((item, index) => (
          <div
            key={index}
            onClick={() => handleWorkFilter(item)}
            className={`app__work-filter-item app__flex p-text ${activeFilter === item ? 'item-active' : ''}`}
          >
            {item}
          </div>
        ))}
      </div>

      <motion.div
        animate={animateCard}
        transition={{ duration: 0.5, delay: 0.5 }}
        className="app__work-portfolio"
      >
        {filterWork.map((work, index) => {
          <div className="app__work-item app__flex" key={index}>
            <div className="app__work-img app__flex">
              <img src={urlFor(work.imgUrl)} alt={work.name} />
            </div>
          </div>
        })}
      </motion.div>
    </>
  )
}

export default Work

Sep 7, 2022, 10:54 AM
Are you trying to render the image on your frontend?
Sep 7, 2022, 12:01 PM
yes, that's right
Sep 8, 2022, 10:23 AM
How do you query your data? what does your query look like?
Sep 8, 2022, 10:26 AM
Ah I see:
*[_type == "works"]
.
Sep 8, 2022, 10:26 AM
You need to either resolve the asset URL as part of your query or use something like @sanity/image-url to get the URL from the asset reference.
Sep 8, 2022, 10:26 AM
You need to either resolve the asset URL as part of your query or use something like @sanity to get the URL from the asset reference. For a case like yours, adjusting your query like this should give you what you want I think. The
image
field (or whatever you want to call it) will contain the URL of your image:
*[_type == "works"] {
  ...,
  "image": nameOfImageField.asset->url
}
Sep 8, 2022, 10:26 AM
And i found a typo… you have a
}
too much in Work before your
return()
Sep 8, 2022, 4:49 PM
Saskia where's that place? I don't see it.
Sep 9, 2022, 9:29 AM
I am so sorry my slack was glitchy on my phone and I misread…. (me culpa!) 🙈
Sep 9, 2022, 12:44 PM
Okay, i found the bugI wrote this:
{filterWork.map((work, index) => {
...
}) }
instead this:
{filterWork.map((work, index) => (
...
))}

these damn curly braces!
Sep 9, 2022, 1:49 PM
DAMN THOSE indeed!
Sep 9, 2022, 3:26 PM
Happy you could solve it and that my wrong hint might have helped anyways 😅
Sep 9, 2022, 3:27 PM

Sanity– build remarkable experiences at scale

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

Was this answer helpful?