javascript - 无法读取未定义的属性“小”
问题描述
当尝试从 API 访问作为图像 src 的链接时,我收到一条错误消息,指出该属性未定义。
以下是 API 中的项目示例,它们都具有相同的属性:
{
"id": 1,
"date": 1606311631,
"title": "A Simple Guide to Design Thinking",
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"thumbnail": {
"large": "https://images.unsplash.com/photo-1454692173233-f4f34c12adad?ixlib=rb-1.2.1&auto=format&fit=crop&w=1180&q=80",
"small": "https://images.unsplash.com/photo-1454692173233-f4f34c12adad?ixlib=rb-1.2.1&auto=format&fit=crop&w=590&q=80"
},
"author": {
"name": "John Doe",
"avatar": "https://images.unsplash.com/photo-1492562080023-ab3db95bfbce?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80",
"role": "Product Owner"
}
},
现在,我映射了 json 中的所有项目,但是当我调用这个函数时:
const AppContainer = (props) => {
return (
<>
{props.data.map((item) => (
<ItemWrapper key={item.id} item={item} />
))}
</>
);
};
const ItemWrapper = (item) => {
return (
<>
<img src={item.thumbnail.small} alt="" />
<span className="blueDot"></span>
<span className="yellowDot"></span>
<div className="title">{item.title}</div>
<div className="content">{item.content}</div>
<div className="authorInfo">
{item.author.name} - {item.author.role}
</div>
<div className="date">{item.date}</div>
</>
);
};
我收到“TypeError:无法读取未定义的属性‘小’”错误。它正在阅读item.thumbnail
,但似乎无处可寻。我应该如何纠正这个?
解决方案
你没有正确解构你的props对象:
const ItemWrapper = ({item})
推荐阅读
- javascript - 根据谷歌表格脚本中的唯一 ID 同步行数据
- java - 为什么我可以使用来自静态内部类的私有构造函数来实例化外部类?
- reactjs - 从 CRA+TS 应用程序的源代码导入时,Cypress 测试无法运行
- twitter-bootstrap-3 - 我收到 ERROR [app-router] Error: Route not found: /pane02 尝试导航到 Aurelia 中 HTML 内的 href
- r - R Shiny with mathjax:如何避免括号自动置于数学模式?
- android - MobileFirst Direct Update 不适用于特定版本
- sql-server - 全局临时表范围在存储过程中的行为不同
- java - 带有自定义键名的 Collectors.groupingBy
- python - 通过 tcp socket 发送和接收的数据不一样
- python - 为什么 Python 中的 range 在执行计算时会在循环中递增 1?