我正在创建一个应用程序,用户可以在其中发布、评论、点赞以及与其他用户的帖子不同。一切都很好,但是当我在帖子中刷新该页面时,我收到一条错误,指出 post.id 中的 id 未定义。然后我从 post.id 中删除 id ——我收到另一个错误,post.title 中的标题未定义并且它继续。
我对编程相当陌生,所以这可能是一件“废话”的事情,但我很沮丧,因为我不明白发生了什么。
非常感谢任何帮助。
应用程序.js
import './App.css';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Navbar from './pages/Navbar';
import LoginPage from './pages/LoginPage';
import PostList from './posts/PostList';
import PostDetail from './posts/PostDetail';
import EditPost from './posts/EditPost';
import Home from './pages/Home';
function App() {
return (
<BrowserRouter>
<Navbar />
<div className="App">
<Routes>
<Route path="/posts" element={<PostList />} />
<Route path="/posts/:id/edit" element={<EditPost/>} />
<Route path="/posts/:id" element={<PostDetail />} />
<Route path="/home" element={<Home />} />
<Route path="/" element={<LoginPage />} />
</Routes>
</div>
</BrowserRouter>
);
}
export default App;
PostDetail.js
import { useContext, useEffect, useState } from "react";
import { Link, useNavigate, useParams } from "react-router-dom";
import { ContentContext } from "../context/ContentContext";
import { UserContext} from "../context/UserContext";
import CommentList from "../comments/CommentList";
import { ErrorContext } from "../context/ErrorContext";
function PostDetail () {
const { setErrors } = useContext(ErrorContext);
const { user } = useContext(UserContext);
const { contents, deletePost } = useContext(ContentContext);
const id = parseInt(useParams().id);
const post = contents.find(post => post.id === id);
const navigate = useNavigate();
const [commentMode, setCommentMode] = useState(false);
const openComment = () => setCommentMode(commentMode => !commentMode);
const [liked, setLiked] = useState(false);
const params = useParams();
useEffect(() => {
fetch(`/posts/${post.id}/likes`)
.then(resp => {
if (resp.ok) {
resp.json().then(data => {
setLiked(data.liked)
})
}
})
.catch(error => {
setErrors(error)
})
}, [post, setErrors])
const handleLike = () => {
fetch(`/posts/${post.id}/likes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})
.then(resp => resp.json())
.then(data => {
if (data.success) {
setLiked(true);
}
})
.catch(error => {
setErrors(error)
});
}
const handleUnlike = () => {
fetch(`/posts/${post.id}/likes/${params.id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
})
.then(resp => resp.json())
.then(data => {
if (data.success) {
setLiked(false);
}
})
.catch(error => {
setErrors(error)
});
}
const onDeletePost = () => {
fetch(`/posts/${post.id}`, {
method: "DELETE",
})
.then(resp => resp.json())
.then(deletePost(post.id))
.then(navigate(`/posts`))
}
return (
<div>
<div className="box-2">
<h1>{post.title}</h1>
<h5>{post.creator.username}</h5>
<p>{post.content}</p>
{liked ? (
<button className="edit-btn" onClick={handleUnlike}>Unlike</button>
):(
<button className="edit-btn" onClick={handleLike}>Like</button>
)}
<button className="edit-btn" onClick={openComment}>Comments</button>
{user && user.username === post.creator?.username && (
<>
<button className="edit-btn"><Link to={`/posts/${post.id}/edit`}>Edit</Link></button>
<button className="edit-btn" onClick={onDeletePost}>Delete</button>
</>
)}
<br /><br />
</div>
<br />
<hr />
<br />
<div>
{commentMode && <CommentList post={post}/>}
</div>
</div>
)
}
export default PostDetail;
我相信问题出在这两个文件中的某个地方。
报错post.id中的id未定义。然后我从 post.id 中删除 id ——我收到另一个错误,post.title 中的标题未定义并且它继续。
您的
post
为空,因为您尚未获取它,或者它不存在。
const post = contents.find(post => post.id === id);
当在数组中找不到 id 时返回 undefined。
在渲染之前检查帖子是否存在:
if (!post) {
return (<p>Loading... (or not found)</p>);
}
return (
<div>
<div className="box-2">