Uncaught TypeError: Cannot read properties of null (reading



下面的错误是react项目的主页在登录过程中,主页变成空白,然后在profilePicUrl图像中给出null属性错误。

return (
<div className='home-container'>
// error
{posts.map((post) => {
return (
<div className='card home-card' key={post._id}>
<h5 style={{ padding: '10px' }}>
{/* console.log("post data", post); */}
<img
className='profilePic'
// error
src={post?.author.profilePicUrl}
alt='Profile Picture'
/>
<Link
to={
post.author._id !== state._id
? '/profile/' + post.author._id
: '/profile'
}>
{post.author.fullName}
</Link>

正如用户Ori Drori指出的那样,尝试添加

src={post?.author?.profilePicUrl}
当react试图访问author时,它可能没有被加载,或者它可能是未定义的。还要确保你的post对象实际上有一个profilePicUrl。

相关内容

最新更新