Watch a live product demo πŸ‘€ See how Sanity powers richer commerce experiences

Console.Log Returning Undefined

19 replies
Last updated: May 16, 2022
import React from 'react';
import { Link } from "react-router-dom";
import Header from "../components/Header";
import "./Home.css";
import {Carousel, Card, Image} from 'antd';
import {client} from "../lib/client";


const Home = ({products,bannerData}) => {

  return(
      <>
        <div className="container">
          <Header/>
          <Carousel autoplay className="carousel">
            {bannerData?.map((e) => {
              return <img src={e} className="carousel-img" alt="carousel"></img>;
            })}
          </Carousel>
          <div className="cards">
            <Card className="card">{products?.map((product)=>{
              return(
                  <div key={product.Slug}>
                  <h1>{product.Name}</h1>
                  <img src={product.Image} alt={product.Name} className="card-content"/>
                  <br />
                  <Link to="/categories" state={product.Genre} className="link">
                    Shop Now
                  </Link>
                  </div>
              )
            })}


            </Card>
          </div>
        </div>
      </>
  )


}
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 }
  }
}

export default Home;

May 16, 2022, 4:55 PM
Hi there! Please keep your question and all relevant information in a single thread. This helps keep the channel clear and allows us to better keep track of your information. πŸ™‚
May 16, 2022, 4:57 PM
It isn't displaying anything. I am mapping the returned prop but it displays nothing. I have data on sanity
May 16, 2022, 4:58 PM
I am following a tutorial, trying sanity for first time. It works for the instructor but it isn't working for me
May 16, 2022, 4:59 PM
What do you get in your console if you add
console.log({ products, bannerData });
on line 10, like this:

const Home = ({products, bannerData}) => {
  console.log({ products, bannerData });
May 16, 2022, 5:01 PM
It returns undefined
May 16, 2022, 5:02 PM
Why is it returning undefined?Did I do a mistake in
getServerSideProps?
May 16, 2022, 5:05 PM
A few things to check:1. If you run those queries in the Vision plugin in your Studio do you get the expected data? If not, the issue is with the query.
2. If you are getting the data in Vision, do you have your client properly configured in your frontend? Have you added your frontend to your CORS origins?
May 16, 2022, 5:07 PM
I didn't add frontend to CORS Origins
May 16, 2022, 5:09 PM
I added frontend to CORS origins
May 16, 2022, 5:11 PM
What is Vision Plugin?
May 16, 2022, 5:11 PM
Vision allows you to query against your dataset inside of your Studio. You should see two options in the black bar at the top of the page: Desk & Vision. If it's not there, you can install it by following these instructions.
May 16, 2022, 5:14 PM
Can i somehow put the fetching inside a useEffect hook?
May 16, 2022, 5:39 PM
Everything else seems to be working
May 16, 2022, 5:39 PM
maybe I am not getting data at frontend
May 16, 2022, 5:39 PM
If you
console.log({ products, bannerData });
in
getServerSideProps
does the data show up? My guess is the client is misconfigured but I would have expected an error in your terminal.
May 16, 2022, 5:52 PM
Data shows up now . Image still isn't showing
May 16, 2022, 5:57 PM
Can anyone paste image schema here
May 16, 2022, 5:58 PM
Looks like I messed up image schema
May 16, 2022, 5:58 PM
Thank everyone issue solved
May 16, 2022, 6:07 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing