当在 commerce js 中访问购物车对象的 line_items 数组时,我收到一条错误,指出它未定义,但我仍然可以在控制台中看到它。
这是一些屏幕截图:
错误:
TypeError: Cannot read property 'length' of undefined
Cart
D:/Zayeed/Projects/e_commerce/src/components/Cart/Cart.jsx:7
4 |
5 | const Cart = ({ cart }) => {
6 | const classes = useStyles();
> 7 | const isEmpty = !cart.line_items.length;
8 |
9 | const EmptyCart = () => (
10 | <Typography variant="subtitle1">You have no items in your cart, start adding some.</Typography
我的代码:
import React from 'react'
import { Container, Typography, Button, Grid } from '@material-ui/core'
import useStyles from './styles';
const Cart = ({ cart }) => {
const classes = useStyles();
const isEmpty = !cart.line_items.length;
const EmptyCart = () => (
<Typography variant="subtitle1">You have no items in your cart, start adding some.</Typography>
);
const FilledCart = () => {
<>
<Grid container spacing={3}>
{cart.line_items.map(item => (
<Grid item xs={12} sm={4} key={item.id}>
<div>{item.name}</div>
</Grid>
))}
</Grid>
<div className={classes.cardDetails}>
<Typography variant="h4">Subtotal: {cart.subtotal.formatted_with_symbol}</Typography>
<div>
<Button className={classes.emptyButton} size="large" type="button" variant="contained" color="secondary">Empty Cart</Button>
<Button className={classes.checkout} size="large" type="button" variant="contained" color="primary">Checkout</Button>
</div>
</div>
</>
}
return (
<Container>
<div className={classes.toolbar}/>
<Typography className={classes.title} variant="h3">Your Shopping Cart:</Typography>
{isEmpty ? <EmptyCart/> : <FilledCart />}
</Container>
)
}
export default Cart
似乎道具没有按预期导入,我无法访问购物车对象的任何属性。
ABOUTYOU
繁华开满天机
BIG阳
慕斯709654
相关分类