Search code examples
reactjsnext.jssanity

fecthing sanity data with next js and reach them , coming empty array,


ı am trying making blog site for me. ı set up nextjs with sanity cli and select blog schema.whenever ı try reach my data on sanity, arrays comes blanks when ı console data ,why? but ı have a couple catogry post like that.

import Image from "next/image";
import sanityClient from "@sanity/client";
import imageUrlBuilder from "@sanity/image-url";

export default function Home({document,arra}) {
  return (
    <div>
      sampple
      {console.log(document,arra)}
    </div>
  );
}

export const client = sanityClient({
  projectId: "c3sj114g",
  dataset: "production",
  apiVersion: "2022-10-02",
  useCdn: true,
  token: process.env.NEXT_PUBLIC_SANITY_TOKEN,
});

export const getServerSideProps = async () => {
  const query = `*[_type == "document"]`;
  const document = await client.fetch(query);

  const arque = `*[_type == "array"]`;
  const arra = await client.fetch(arque);
  return {
    props: { document,arra},
  };
};

Solution

  • Over here the _type you are passing in the GROQ query is incorrect. The _type should be the the name of the schema, of type document.

    So, if the name of your schema of type document is ingredient, then you should write the query like below:

    const query = `*[_type == "ingredient"]`;