#Noob question about fetching data

4 messages ยท Page 1 of 1 (latest)

stone fulcrum
#

Hi all ๐Ÿ™‚

Got a noob question as I'm learning (more) about Next.js on the fly.

I'm fetching data (see below) and trying to access it in my component. However, the data won't show? Upon console.log'ing, I see it's undefined. No idea why. Again, prolly a rookie mistake but I'm just trying to understand (:

export const fetchData = async () => {
  // From the Sanity tutorial, trying to fetch pets
  const pets = await client.fetch(`*[_type == "pet"]`);

  return {
    props: {
      pets,
    },
  };
};

So I have two questions, actually

  1. How can I 'know' what the type is of the returned props, namely pets. Because I'm using TS
  2. Maybe if I know 1 I know 2, but still sometimes a bit lost with undefined and all, why is it undefined?

Any help is much appreciated! ๐Ÿ™‚

trail heartBOT
#

๐Ÿ”Ž This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      ๐Ÿ•ต๏ธ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      โœ… You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
rancid plank
#

you need to use getServerSideProps() in a page to allow prefetching data of a page