Console.Log Returning Undefined
19 replies
Last updated: May 16, 2022
M
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
M
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
M
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
M
It returns undefined
May 16, 2022, 5:02 PM
M
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?
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
M
I didn't add frontend to CORS Origins
May 16, 2022, 5:09 PM
M
I added frontend to CORS origins
May 16, 2022, 5:11 PM
M
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
M
Can i somehow put the fetching inside a useEffect hook?
May 16, 2022, 5:39 PM
M
Everything else seems to be working
May 16, 2022, 5:39 PM
M
maybe I am not getting data at frontend
May 16, 2022, 5:39 PM
If you
console.log({ products, bannerData });in
getServerSidePropsdoes 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
M
Data shows up now . Image still isn't showing
May 16, 2022, 5:57 PM
M
Can anyone paste image schema here
May 16, 2022, 5:58 PM
M
Looks like I messed up image schema
May 16, 2022, 5:58 PM
M
Thank everyone issue solved
May 16, 2022, 6:07 PM
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.