javascript - TypeError:无法读取未定义的属性(读取“标题”)
问题描述
我面临一个奇怪的错误!我正在尝试在我的 UI 上动态呈现产品。当我的代码中有:
render () {
const cart = this.props.cart
return (
<>
<PanelHeader size="sm" />
<div className="content">
<Row>
<Col xs={12}>
<Card>
<CardHeader>
<CardTitle tag="h4">Products List</CardTitle>
</CardHeader>
<CardBody>
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col"><strong>#</strong></th>
<th scope="col"><strong>Name</strong></th>
<th scope="col"><strong>Code Item</strong></th>
<th scope="col"><strong>Quantity</strong></th>
<th scope="col"><strong>Price Total</strong></th>
</tr>
</thead>
{cart.length > 0 &&
<tbody>
<tr>
<th scope="row">1</th>
<td>{cart[0].title}</td>
<td>{cart[0].code}</td>
<td>{cart[0].quantity}</td>
<td>{cart[0].price}</td>
</tr>
</tbody>}
</table>
</CardBody>
</Card>
</Col>
</Row>
</div>
</>
)
}
}
它呈现第一个项目,但是如果我尝试选择第三个项目:即 {cart[2].title} 等等会给我“标题”未定义的错误。另外,如果我尝试给 [i] 给我一个“i”未定义的错误!任何想法如何解决它?先感谢您!
解决方案
如果您需要渲染购物车数组中的所有商品,请使用:
{cart.map(c =>
<tbody>
<tr>
<th scope="row">1</th>
<td>{c.title}</td>
<td>{c.code}</td>
<td>{c.quantity}</td>
<td>{c.price}</td>
</tr>
</tbody>}
推荐阅读
- c++ - 如何在 C++ 中删除输出末尾的空格?
- python - 如何对 seaborn relplot 的轴使用对数刻度?
- r - 如何在重复元素具有相同顺序的向量中查找元素的顺序?
- r - 编码一个函数
- c - 我是初学者,此代码需要提供属性详细信息。它会自动选择属性类型为房屋。我该怎么办?
- python - 将一列转换为 Pandas 中的列标题对于同一变量的每个类的多个观察
- javascript - 需要 Javascript 重构帮助
- python - NumPy:增加一列中的值
- asp.net-core-mvc - Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(对象值)
- android - Kotlin-我怎样才能像这样 1,000,000,000 每三个数字加上逗号