简体   繁体   English

我收到错误“TypeError:products.map 不是函数”

[英]I got the error "TypeError: products.map is not a function"

This error is appears in the Home page of the website.此错误出现在网站的主页中。 When I'm in another page and click the back button to open the Home Page this error appears.当我在另一个页面并单击后退按钮打开主页时,会出现此错误。 But when I'm refreshing the Home page, this error despairs.但是当我刷新主页时,这个错误令人绝望。 This is the Code that I used in home page这是我在主页中使用的代码

        import React, { useState, useEffect } from 'react';
        import { Link } from 'react-router-dom'
        import axios from 'axios';
        import { useSelector, useDispatch } from 'react-redux';
        import { listProducts } from '../actions/productActions';

        function HomeScreen(props) { 
            const productList = useSelector(state => state.productList);
            const { products, loading, error } = productList;
            const dispatch = useDispatch();
            
            useEffect(() => {
                dispatch(listProducts());
                return () =>{
                    //
                };
            }, []) 

            return loading ? <div>Loading...</div> : 
                error ? <div>{error}</div> :
            
            <ul className="products"> 
            {
                products.map(product =>
            <li key={product._id}>
                    <div className="product">
                    <Link to={'/product/' + product._id}>  
                        <img className="products-image" src={product.image} alt="product"/>
                        </Link>
                        <div className="product-name">
                            <Link to= {'/product/' + product._id}> {product.name} </Link>
                            </div>
                        <div className="product-brand">{product.brand}</div>
                        <div className="product-price">Rs.{product.price}</div>  
                        <div className="product-rating">{product.rating} Stars ({product.numReivews}Reviews)</div>
                    </div>
                </li>) 
            }
                    
            </ul>
        
        }
        export default HomeScreen;

The main reason behind is .map() can be used only on arrays. You can use null or undefined checks before using .map() as:背后的主要原因是.map()只能在 arrays 上使用。您可以在使用.map()之前使用nullundefined检查:

<ul className="products"> 
  {
     products && products.map(product =>
        <li key={product._id}>
           { /* rest of the code */ }
        </li>) 
  }
</ul>

+1 suggestion: +1 建议:

Also it is worth to check the type of productList is in the store, maybe initially it's an empty object. Although clearly it has to be an array to iterate through on it with .map() .同样值得检查商店中productList的类型,也许最初它是一个空的 object。尽管很明显它必须是一个数组才能使用.map()对其进行迭代。 Thus I would use an empty array as [] or null .因此,我会使用一个空数组作为[]null

暂无
暂无

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

相关问题 类型错误:products.map 不是 function - TypeError: products.map is not a function 未捕获的类型错误:products.map 不是 function、ReactJS - Uncaught TypeError: products.map is not a function, ReactJS 类型错误:products.map 不是 function - React JS - TypeError: products.map is not a function - React JS TypeError: products.map 不是 nextjs 中的 function - TypeError: products.map is not a function in nextjs 错误:products.map 不是函数 Mern Stack - Error: products.map is not a function Mern Stack React 获取数据的问题 Uncaught TypeError: products.map is not a function - React problem fetching data Uncaught TypeError: products.map is not a function UncaughtTypeError products.map 不是 function - UncaughtTypeError products.map is not a function 我收到一个错误:未捕获(承诺)类型错误:cartItems.map 不是 function。 上述错误发生在<cartscreen>零件:</cartscreen> - i got an error: Uncaught (in promise) TypeError: cartItems.map is not a function. The above error occurred in the <CartScreen> component: 我创建了这个 React 应用程序来获取我创建的 API,但收到错误消息“TypeError: video.map is not a function”。 下面是代码: - I created this React application to a fetch API that I created, but got the error message "TypeError: video.map is not a function". Below is the code: 为什么我有这个错误“Uncaught TypeError: posts.map is not a function” - Why I have this error "Uncaught TypeError: posts.map is not a function"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM