提问人:Adeniyi Emily Adejoke 提问时间:11/17/2023 最后编辑:Drew ReeseAdeniyi Emily Adejoke 更新时间:11/18/2023 访问量:19
typeError:无法读取未定义的 reading('title')-in- vite react 的属性
typeError: Cannot read properties of undefined reading('title')-in- vite react
问:
我在使用 vite-react 时在控制台中遇到此错误。我能够从主组件导航到此组件,并且从MongoDB日志中获取了来自控制台的ID,但无法获取特定ID的数据。我是初学者。感谢您的帮助。
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom"
import axios from "axios";
function BlogDetails() {
const id = useParams().id;
console.log(id)
const [post, setPost]=useState([])
const fetchBlogById = async () => {
const res = await axios.get(`http://localhost:8000/user/${id}`)
console.log('error fectching the data');
const data = await res.data
return data
}
useEffect(() =>{
fetchBlogById().then((data) => setPost(data.post))
}, [id]);
console.log(post)
return (
<div className="blog-details">
<h2>{post.title}</h2>
<p>Written by {post.author}</p>
<div>{post.content}</div>
</div>
)
}
答: 暂无答案
评论
post
useEffect
data
data.post