简体   繁体   English

×获取TypeError:无法读取未定义的属性“ map”

[英]× Getting a TypeError: Cannot read property 'map' of undefined

I'm using react to build the front end and am trying to diaplay a list of products.我正在使用React构建前端,并尝试显示产品列表。

I am getting a type error of map undefined whenever I run the code.每当我运行代码时,我都会得到未定义的map类型错误。

I thought I had declared it already using this.我以为我已经使用它声明了它。

const productList = useSelector(state => state.productList);
const {products, loading, error} = productList;



  return loading ? <div>Loading...</div>:
  error ? <div>{error}</div>:
     <ul class = "products">
    { products.map(product =>
          <li key={product._id}>
            <div className="product">
            <Link to={'/product/' + product._id}>
            <img className="product-image" src={product.image} alt="Monkey light"/>
            </Link>
               <div className="product-heading">
                <Link to={'/product/' + product._id}>{product.name}</Link>
                </div>
               <div className="product-brand">{product.brand}</div>
               <div className="product-price">{product.price}</div>
               <div className="product-rating">{product.rating} {product.numReviews}</div>
              </div>
          </li>
       )}
    </ul>
    }

The productList is defined within the function. productList在函数中定义。

I'm using react to build the front end and am trying to diaplay a list of products.我正在使用React构建前端,并尝试显示产品列表。

I am getting a type error of map undefined whenever I run the code.每当我运行代码时,我都会得到未定义的map类型错误。

I thought I had declared it already using this.我以为我已经使用它声明了它。

const productList = useSelector(state => state.productList);
const {products, loading, error} = productList;



  return loading ? <div>Loading...</div>:
  error ? <div>{error}</div>:
     <ul class = "products">
    { products.map(product =>
          <li key={product._id}>
            <div className="product">
            <Link to={'/product/' + product._id}>
            <img className="product-image" src={product.image} alt="Monkey light"/>
            </Link>
               <div className="product-heading">
                <Link to={'/product/' + product._id}>{product.name}</Link>
                </div>
               <div className="product-brand">{product.brand}</div>
               <div className="product-price">{product.price}</div>
               <div className="product-rating">{product.rating} {product.numReviews}</div>
              </div>
          </li>
       )}
    </ul>
    }

The productList is defined within the function. productList在函数中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM