【发布时间】:2020-08-31 13:29:16
【问题描述】:
我有一个有趣的错误。 在这里,我有一个带有 axios 的反应文件,它从我的帖子中获取数据,即使它适用于显示我在一个页面中发布所有帖子的所有帖子,我也有问题,我试图找到解决方案但找不到。
这是我的反应代码
const [post, setPost] = useState([]);
var id = match.params.id;
useEffect(() => {
axios.get('http://localhost:3001/post/' + id).then(res => {
setPost(res.data);
}).catch(err => {
console.log(err);
})
}, []);
return (
<div >
<div className="blogcontainer">
<img src={post.images.destination + post.images.filename} alt={post.name} />
<div className="articleWrapper">
<h2>{post.name}</h2>
{parse(String(post.postcontent))}
</div>
</div>
</div>
)
它需要的数据是:
data: {…}
__v: 0
_id: "5f4adaa46bde7743b4254615"
createdAt: "2020-08-29T22:45:56.894Z"
description: "At first write an introduction without pushing enter until complete introduction..."
images: {…}
destination: "uploads/"
encoding: "7bit"
fieldname: "mainImage"
filename: "1598741156885.jpg"
mimetype: "image/png"
originalname: "iguda_logo.png"
path: "uploads\\1598741156885.jpg"
size: 758685
<prototype>: Object { … }
name: "fdhfgh"
postcontent: "<p>At first write an introduction without pushing enter until complete introduction</p>"
这是错误:
TypeError: post.images is undefined
BlogArticle
C:/Users/arsla/OneDrive/Documents/GitHub/inc-web/client/src/component/BlogArticle.jsx:23
20 | return (
21 | <div >
22 | <div className="blogcontainer">
> 23 | <img src={post.images.destination + post.images.filename} alt={post.name} />
| ^ 24 | <div className="articleWrapper">
25 | <h2>{post.name}</h2>
26 | {parse(String(post.postcontent))}
谢谢。
【问题讨论】:
标签: javascript reactjs express axios