firebase 我正在使用Sanity管理数据库,但我有一个问题,显示我的网站上的项目...这里是代码

bnl4lu3b  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(81)

为什么项目不显示在我的网站上?我做错了什么家伙?有人能帮助我或告诉我如何解决这个问题的代码块(index.js)

import { client } from '../lib/client';
import {HeroBanner,FooterBanner, Product} from '../components';
const Home = ({ products, bannerData}) => (
  <div>
    <HeroBanner heroBanner ={bannerData.length && bannerData[0]} />
    <div className="products-heading">
      <h2>Best Selling Product</h2>
      <p>Speakers of many variations</p>
    </div>
    
    <div className="products-container">
    {products?.map((product) =><Product key={product.id} product={product.name}/>)}
    </div>

    <FooterBanner footerBanner={bannerData && bannerData[0]}/>
  </div>
);

export const getServerSideProps = async () =>{
  const query = '*[_type == "product"]';
  const product = await client.fetch(query);

  const bannerQuery = '*[_type == "banner"]';
  const bannerData = await client.fetch(bannerQuery);

  return{
    props: {product, bannerData }
  }
}

export default Home;```
vwoqyblh

vwoqyblh1#

错误是您正在通过getServerSideProps函数传递属性product。您需要正确地检索它。

const Home = ({ products /* <---- Here you are trying to get 'products' instead of 'product' */, bannerData}) => {
    
    return (
         {/* ... */}
    );
};

export async function getServerSideProps() {
  
  //...

  return {
    props: { product, bannerData } // <----- Here you are passing 'product'
  }
}

相关问题