javascript - 如何解决 var.map is not a function TypeError?
问题描述
我正在为我的网站开发一个博客页面,该页面基于 Next.js,使用 TypeScript 编写,并使用 Strapi 作为 CMS API。
下面的代码片段给了我一个服务器错误,TypeError: posts.map is not a function
我不知道为什么。
type BlogPageProps = {
posts: PostData[];
};
const BlogPage = ({ posts }: BlogPageProps) => {
useEffect(() => {
AOS.init({ duration: 700 });
}, []);
return (
<>
<SEO title={`Blog | ${SITE_NAME}`} />
<AnimationContainer animation="appearFromAbove">
{posts.map((post) => (
<PostCard effect="fade-up" key={post.id} post={post} />
))}
</AnimationContainer>
</>
);
};
export default BlogPage;
PostData[] 只是一种由以下参数组成的类型:
export type PostData = {
id: PostID;
title: string;
content: string;
slug: string;
author: PostAuthor;
category: PostCategory;
created_by: PostCreatedBy;
updated_by: PostCreatedBy;
created_at: string;
updated_at: string;
cover: PostCover;
};
我认为这与 API 实例本身的代码或某些依赖问题有关?如果有人可以帮助我,我将不胜感激。谢谢你。
解决方案
首先控制台帖子并检查它是否未定义,null,或者帖子类型必须是数组
我认为你应该尝试关注
post.posts.map()
推荐阅读
- javascript - 用 js 和增量符号 (++) 克隆一个项目,我的双增量错误 i
- python - Pandas:根据 if-else 语句和来自另一个数据帧的值在数据帧中创建列
- .net-core - 连续作业队列在关闭前完成作业
- angular - Angular - 在使用哈希更改 URL 后阻止 ngOnInit 调用
- python - Plotly Sankey:为什么节点放错了位置?
- r - 添加或删除文本输入行
- python - 与 1 的补码二进制表示混淆
- ruby - Ruby Action Mailer 在开发中不会发送到 smtp
- php - php - 使用 odbc_fetch_array 的索引数组错误
- kotlin - Kotlin:预期 ')' 错误,代码未运行