[英]i got an error: Uncaught (in promise) TypeError: cartItems.map is not a function. The above error occurred in the <CartScreen> component:
i am following a mern stack tutorial of creating a full stack web application using react and i got an error in cartItems.map-- Uncaught (in promise) TypeError: cartItems.map is not a function.我正在关注使用 react 创建完整堆栈 web 应用程序的 mern 堆栈教程,但我在 cartItems.map 中遇到错误--未捕获(承诺)TypeError:cartItems.map 不是 ZC1C425268E68384F1CAB4A57。 The above error occurred in the component: , i guess it's because of an array and i have tried to make it an array but it was also not working for me
上面的错误发生在组件中:,我猜这是因为一个数组,我试图把它变成一个数组,但它也对我不起作用
import React, { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { Link, useLocation, useNavigate, useParams } from 'react-router-dom'; import { addToCart } from '../actions/cartActions'; import MessageBox from '../components/MessageBox'; export default function CartScreen(props) { const params = useParams(); const productId = params.id; const {search} =useLocation(); const qtyInUrl = new URLSearchParams(search).get('qty'); const qty = qtyInUrl?Number(qtyInUrl):1; const cart = useSelector(state => state.cart); const {cartItems } = cart; const{ error} = cart; const dispatch = useDispatch(); useEffect(() => { if (productId) { dispatch(addToCart(productId, qty)); } }, [dispatch, productId, qty]); const removeFromCartHandler = () => { // delete action // dispatch(removeFromCart(id)); }; const navigate=useNavigate() const checkoutHandler = () => { navigate('/signin?redirect=shipping'); }; return (<div className="row top"> <div className="col-2"> <h1>Shopping Cart</h1> {error && <MessageBox variant="danger">{error}</MessageBox>} {cartItems.length === 0? ( <MessageBox> Cart is empty. <Link to="/">Go Shopping</Link> </MessageBox> ): ( <ul> {cartItems.map((item) => ( <li key={item.product}> <div className="row"> <div> <img src={item.image} alt={item.name} className="small" ></img> </div> <div className="min-30"> <Link to={`/product/${item.product}`}>{item.name}</Link> </div> <div> <select value={item.qty} onChange={(e) => dispatch( addToCart(item.product, Number(e.target.value)) ) } > {[...Array(item.countInStock).keys()].map((x) => ( <option key={x + 1} value={x + 1}> {x + 1} </option> ))} </select> </div> <div>${item.price}</div> <div> <button type="button" onClick={() => removeFromCartHandler(item.product)} > Delete </button> </div> </div> </li> ))} </ul> )} </div> <div className="col-1"> <div className="card card-body"> <ul> <li> <h2> Subtotal ({cartItems.reduce((a, c) => a + c.qty, 0)} items): $ {cartItems.reduce((a, c) => a + c.price * c.qty, 0)} </h2> </li> <li> <button type="button" onClick={checkoutHandler} className="primary block" disabled={cartItems.length === 0} > Proceed to Checkout </button> </li> </ul> </div> </div> </div> ); }
This error occurs because your data is not an array.发生此错误是因为您的数据不是数组。 The.map() function only works with arrays.
The.map() function 仅适用于 arrays。 First, you'll need to confirm your data type.
首先,您需要确认您的数据类型。 Second, you'll need to transform your data into an array.
其次,您需要将数据转换为数组。
You should check your cartItems type and change your state to be sure to have an array.您应该检查您的 cartItems 类型并更改您的 state 以确保有一个数组。
source: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Map来源: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Map
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.