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.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.