Error fetching data from Sanity using Next.js and getServerSideProps

9 replies
Last updated: May 11, 2022
HI! Yesterday I followed ta YT tutorial on sanity and something else. This is my first time using sanity, so I spent roughly 24 hours trying to fix the error I got. Essentially I, am exporting an object using SanityClient just like in docs using the correct projectId and token.
import sanityClient from '@sanity/client';
import imageUrlBuilder from '@sanity/image-url';

export const client = sanityClient({
  projectId: '********', // commented out intentionaly
  dataset: 'production',
  apiVersion: '2022-05-08',
  useCdn: true,
  token: process.env.NEXT_PUBLIC_SANITY_TOKEN
});

const builder = imageUrlBuilder(client);

export const urlFor = (source) => builder.image(source);
Then I created two schemas for which I also created entries in my
sanity.io/desk and imported them to schema.js file
import createSchema from 'part:@sanity/base/schema-creator';

import schemaTypes from 'all:part:@sanity/base/schema-type';

import product from './product';
import banner from './banner';

export default createSchema({
  name: 'default',
  types: schemaTypes.concat([ product, banner ]),
})
I am using Next.js so I used
getServerSideProps
to fetch the data this way:
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 },
  };
};
However, when I console.log(products) I get returned an empty array without any errors. Do you know what went wrong or at least a way to debug this? I cannot seem to get any data back from sanity.
May 9, 2022, 7:02 PM
Hey there! You'll want to change your queries from
type
to
_type
.
May 9, 2022, 7:15 PM
I can't believe, it worked! Can't believe it was that small of an error. Thank you so much!šŸ™‚
May 9, 2022, 7:20 PM
You're welcome! Glad it was such a quick fix! šŸ‘Š
May 9, 2022, 7:21 PM
user E

You probably do not want your token to be publicly available with
process.env.NEXT_PUBLIC_SANITY_TOKEN
May 9, 2022, 8:51 PM
Isn't it enough that I added it to the .env file and added that to gitignore?
May 10, 2022, 1:06 AM
Variables prefixed with
NEXT_PUBLIC
will be exposed to the client and added to the build.
You should read up on
https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables-to-the-browser
May 10, 2022, 1:10 AM
Haven't personally tested this, but I believe you can just
console.log(NEXT_PUBLIC_SANITY_TOKEN)
in the browser console and anyone can easily retrieve your token and get access to your Sanity account
May 10, 2022, 1:17 AM
Just tested this myself, and can confirm that you can see the API key in clear text in Network->Response (if you use Firefox)
May 10, 2022, 1:26 AM
Thank you so much for this, I'll read up!
May 11, 2022, 1:02 AM

Sanityā€“ build remarkable experiences at scale

Sanity is a modern 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?