简体   繁体   English

我收到一个错误:未捕获(承诺)类型错误: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:

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.

相关问题 javascript - Uncaught (in promise) TypeError: e.iterator is not a function。 如何修复此类错误? - javascript - Uncaught (in promise) TypeError: e.iterator is not a function. How to fix such error? Uncaught TypeError: Cannot read properties of undefined (reading 'pathname') and 上述错误发生在<link>零件: - Uncaught TypeError: Cannot read properties of undefined (reading 'pathname') and The above error occurred in the <Link> component: 使用 Fetch API 时,我收到此错误: Uncaught (in promise) TypeError: packages.map is not a function - When using Fetch API I get this error: Uncaught (in promise) TypeError: packages.map is not a function 未捕获的类型错误:data.map 不是 function --&gt;反应组件错误 - Uncaught TypeError: data.map is not a function -->react component error “未捕获(承诺)TypeError:_this.setState 不是函数”错误 - "Uncaught (in promise) TypeError: _this.setState is not a function" error 错误:未捕获(承诺):TypeError:r不是函数 - Error: Uncaught (in promise): TypeError: r is not a function Uncaught Promise错误:TypeError:member不是函数 - Uncaught Promise Error: TypeError: member is not a function 未捕获的TypeError:data.push不是函数。 我怎样才能解决这个推送错误? - Uncaught TypeError: data.push is not a function. How can I get around this push error? 为什么我有这个错误“Uncaught TypeError: posts.map is not a function” - Why I have this error "Uncaught TypeError: posts.map is not a function" 我收到错误“TypeError:products.map 不是函数” - I got the error "TypeError: products.map is not a function"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM