javascript - React.js 错误:TypeError:无法读取未定义的属性“地图”
问题描述
目前正在尝试从本教程制作一个 MERN Stack Web 应用程序:https : //youtu.be/aibtHnbeuio 所以我对堆栈也很陌生,不知道如何解决这个问题......请帮助
TypeError:无法读取未定义帖子 C:/Users/I/Desktop/memories-pr/client/src/components/Posts/Post/Post.js:27的属性“地图”
24 | </Button>
25 | </div>
26 | <div className={classes.details}>
> 27 | <Typography variant="body2" color="textSecondary" component="h2">{post.tags.map((tag) => `#${tag} `)}</Typography>
| 28 | </div>
29 | <CardContent>
30 | <Typography className={classes.title} variant="h5" gutterBottom > {post.message} </Typography>
(匿名函数)C:/Users/I/Desktop/memories-pr/client/src/actions/posts.js:8
5 | try {
6 | const { data } = await api.fetchPosts();
7 |
> 8 | dispatch({ type: 'FETCH_ALL', payload: data});
| ^ 9 | } catch (error) {
10 | console.log(error.message);
11 | }
解决方案
这可能是由post
异步获取引起的,如果是这种情况,那么最初的值post
将保持不变undefined
,您将看到此错误。
使用空传播或条件渲染来避免此错误:
<Typography
variant="body2"
color="textSecondary"
component="h2">
{post?.tags?.map((tag) => `#${tag} `)}
</Typography>
推荐阅读
- reactjs - Mui Spacing V4 不起作用。尽管有新的间距枚举,间距似乎被网格大小覆盖
- python - 如何摆脱文本文件中每一行前面的“/n”字符?
- sql - 在 T-SQL 过程中动态迭代传入的参数值
- node.js - 是否可以通过安装在 Heroku 上的不和谐机器人播放音乐/声音?
- postgresql - 如何在 PostgreSQL 中引发特定异常
- javascript - 引导 Strapi 角色权限
- spring - spring boot 测试 application.properties 文件未找到
- mongodb - 如何以任何顺序检索与数组项匹配的 mongo 集合?
- python - 使用 python 静态类型/mpy,我可以说“此方法与方法 X 具有相同/相似的类型签名”吗?
- javascript - 如何在 Phaser JS 中实现冲刺能力?