Issue with retrieving data from an array in a document in Sanity.io

11 replies
Last updated: Oct 4, 2021
pls urgent assistant needed thanks🙏
Sep 28, 2021, 12:46 PM
Hi Victor, could you try publishing your document to see if that gives you data? You may not be able to read unpublished data if you're using an unauthenticated client.
If that's not the issue, could you share the relevant query?
Sep 28, 2021, 2:26 PM
ok thanks
Sep 28, 2021, 2:59 PM
am getting the other items except the images array that is showing empty object
Sep 28, 2021, 3:01 PM
export default {  name: "home",
  title: "Home",
  type: "document",
  fields: [
    {
      name: "banner",
      title: "Banner ",
      type: "string",
    },
    {
      name: "title",
      title: "Banner Title",
      type: "string",
    },
    {
      name: "description",
      title: "Banner Description",
      type: "string",
    },
    {
      name: "images",
      title: "Images",
      type: "array",
      of: [
        {
          name: "image",
          type: "image",
        },
      ],
    },
    {
      name: "weddingDate",
      title: "Wedding Date",
      type: "date",
      options: {
        dateFormat: "YYYY-MM-DD",
        calendarTodayLabel: "Today",
      },
    },
  ],
};
Sep 28, 2021, 3:01 PM
useEffect(() => {    sanityClient
      .fetch(
        
*[_type == "home"]{
        title,
        weddingDate,
        description,
        images{
          asset ->{
            _id,
            url
          },
        },
  
      }
      )
      .then((data) => setSlide(data))
      .catch(console.error);
  }, []);
Sep 28, 2021, 3:02 PM
Hi Victor. Since
images
is an array, could you try appending
[]
in your query?

`*[_type == "home"]{
  title,
  weddingDate,
  description,
  images[]{
    asset->{
    // ...
`
Sep 28, 2021, 4:56 PM
thanks very much... you made my day❤️
Sep 29, 2021, 9:51 AM
Glad you got things working, Victor. 🙂
Sep 29, 2021, 2:32 PM
i dont know why am having this error
Oct 4, 2021, 2:08 PM
Error: Objects are not valid as a React child (found: object with keys {_key, _type, children, markDefs, style}). If you meant to render a collection of children, use an array instead.
Oct 4, 2021, 2:08 PM
useEffect(() => {    sanityClient
      .fetch(
        
*[_type == "home"]{
        title,
        weddingDate,
        description,
        coupleHighlight,
        bannerImages[]{
          asset ->{
            _id,
            url
          },
        },
       
        coupleIntroImage[]{
          asset ->{
            _id,
            url
          },
        },
        coupleIntroDescription,
      }
      )
      .then((data) => setSlide(data))
      .catch(console.error);
  }, []);
Oct 4, 2021, 2:09 PM

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?