简体   繁体   English

无法读取未定义的属性(读取“地图”)反应 JS

[英]Cannot read properties of undefined (reading 'map') react JS

I am trying to make a blog using ReactJS and NextJS in VS Code I don't have any error but when I run it it shows in browser: " TypeError: Cannot read properties of undefined (reading 'map') "我正在尝试在 VS Code 中使用 ReactJS 和 NextJS 创建一个博客,但没有任何错误,但是当我运行它时,它会在浏览器中显示:“TypeError: Cannot read properties of undefined (reading 'map')”

So this is the code所以这是代码

 import React, { useState, useEffect } from 'react'; import Image from 'next/image'; import moment from 'moment'; import Link from 'next/link'; import { getSimilarPosts, getRecentPosts } from '../services'; const PostWidget = ({ categories, slug }) => { const [relatedPosts, setRelatedPosts] = useState([]); useEffect(() => { if (slug) { getSimilarPosts(categories, slug).then((result) => { setRelatedPosts(result); }); } else { getRecentPosts().then((result) => { setRelatedPosts(result); }); } }, [slug]) console.log(relatedPosts) return ( <div className="bg-white shadow-lg rounded-lg p-8 pb-12 mb-8"> <h3 className="text-xl mb-8 font-semibold border-b pb-4">{slug ? 'Related Posts' : 'Recent Posts'}</h3> {relatedPosts.map((post) => ( <div key={post.title} className="flex items-center w-full mb-4"> <div className="w-16 flex-none"> <img alt={post.title} height="60px" width="60px" unoptimized className="align-middle rounded-full" src={post.featuredImage.url} /> </div> <div className="flex-grow ml-4"> <p className="text-gray-500 font-xs">{moment(post.createdAt).format('MMM DD, YYYY')}</p> <Link href={`/post/${post.slug}`} className="text-md" key={index}>{post.title}</Link> </div> </div> ))} </div> ); }; export default PostWidget;

And this is where the error is.这就是错误所在。

{relatedPosts.map((post) =>

use ?使用? make in this code:在这段代码中制作:

{relatedPosts?.map((post) =>

Check relatedPosts has data before rendering map.在渲染地图之前检查relatedPosts是否有数据。 If not working please share getSimilarPosts, getRecentPosts如果不起作用,请分享getSimilarPosts、getRecentPosts

{
   relatedPosts && 
   relatedPosts.length > 0 &&
   relatedPosts.map((post) => (
      ...
   ))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性(读取“地图”)React JS - TypeError: Cannot read properties of undefined (reading 'map') React JS TypeError:无法在反应 JS 中读取未定义的属性(读取“地图”) - TypeError: Cannot read properties of undefined (reading 'map') in react JS React.map 错误(无法读取未定义的属性(读取“地图”)) - React .map error (Cannot read properties of undefined (reading 'map')) react-refresh-runtime.development.js:315 Uncaught TypeError: Cannot read properties of undefined (reading 'map') - react-refresh-runtime.development.js:315 Uncaught TypeError: Cannot read properties of undefined (reading 'map') 使用redux时无法在react js中读取未定义(读取&#39;map&#39;)的属性 - Cannot read properties of undefined (reading 'map') in react js while using redux 我该如何解决这个问题? 类型错误:无法读取 react.js 中未定义(读取“地图”)的属性 - How can i solve this issue ? TypeError: Cannot read properties of undefined (reading 'map') in react.js 反应:类型错误:无法读取未定义的属性(读取“地图”) - React: TypeError: Cannot read properties of undefined (reading 'map') 反应问题无法在测验应用程序中读取未定义的属性(读取“地图”)? - React problem Cannot read properties of undefined (reading 'map') in a quiz app? 未捕获的类型错误:无法读取未定义的属性(读取“地图”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'map') React TypeError:无法在反应中读取未定义(读取“地图”)的属性 - TypeError: Cannot read properties of undefined (reading 'map') in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM