Why is fetch undefined for Sanity client?

Viewed 32

For some reason I am not able to perform a fetch using Sanity.io with Next.js. Here is me initializing the client:

import sanityClient from "@sanity/client";
export const client = sanityClient({
  projectId: "sdnki6rg",
  dataset: "production",
  apiVersion: "2022-03-10",
  useCdn: true,
  token: process.env.NEXT_PUBLIC_SANITY_TOKEN,
});

Then I import it into my index page and try to perform some queries:

import client from "../lib/client";
export const getServerSideProps = async () => {
  const query = "*[_type == 'product']";
  const products = await client.fetch(query);

  const bannerQuery = "*[_type == 'banner']";
  const bannerData = await client.fetch(bannerQuery);
  return {
    props: { products, bannerData },
  };
};

My next.js dev server says: "TypeError: Cannot read properties of undefined (reading 'fetch')". I believe I have all the proper dependencies installed so I'm not sure why this isn't working.

0 Answers
Related