无法删除对 post 的评论?
Failed to delete the a comment on post?
我正在尝试删除对 post 的评论,但找不到该评论。当我 console.log(post.comments)
时,它会显示所有评论,但我仍然找不到评论。错误是 Comment not found
我写的是为了发现评论是否仍然存在。但是评论在那里,我将 id 与它匹配。帮助我,我是 NodeJs 的新手。帮我解决这个问题
*作为前端,我正在使用 react 和 redux 我认为问题出在后端,我还用 postman 进行了测试。无法删除来自 postman.
的评论
这里是评论路由和控制器
router.route('/:id/comment/:comment_id').delete(protect, deleteComment);
export const deleteComment = asyncHandler(async (req, res) => {
const post = await Post.findById(req.params.id);
const comment = post.comments.find(
(comment) => comment._id === req.params.comment_id
);
if (!comment) {
res.status(404);
throw new Error('Comment not found');
}
//Check User
if (comment.user.toString() === req.user._id.toString()) {
post.comments = post.comments.filter(
({ id }) => id !== req.params.comment_id
);
await post.save();
return res.json(post.comments);
} else {
res.status(401);
throw new Error('User not authorized');
}
});
这里是 post 模型
import mongoose from 'mongoose';
const postSchema = mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: [true, 'Please Author is required'],
},
title: {
type: String,
required: true,
},
desc: {
type: String,
required: true,
},
img: {
type: String,
},
isLiked: {
type: Boolean,
default: false,
},
isDisLiked: {
type: Boolean,
default: false,
},
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
disLikes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
comments: [
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
text: {
type: String,
required: true,
},
name: {
type: String,
},
pic: {
type: String,
},
date: {
type: Date,
default: Date.now,
},
},
],
categories: {
type: Array,
},
},
{
timestamps: { createdAt: 'created_at', updatedAt: 'modified_at' },
}
);
const Post = mongoose.model('Post', postSchema);
export default Post;
当您访问 _id
时,您正在访问 ObjectId
的实例
您应该尝试与 id
进行比较,这是 _id
的字符串表示形式
const comment = post.comments.find(
(comment) => comment.id === req.params.comment_id
);
我正在尝试删除对 post 的评论,但找不到该评论。当我 console.log(post.comments)
时,它会显示所有评论,但我仍然找不到评论。错误是 Comment not found
我写的是为了发现评论是否仍然存在。但是评论在那里,我将 id 与它匹配。帮助我,我是 NodeJs 的新手。帮我解决这个问题
*作为前端,我正在使用 react 和 redux 我认为问题出在后端,我还用 postman 进行了测试。无法删除来自 postman.
的评论这里是评论路由和控制器
router.route('/:id/comment/:comment_id').delete(protect, deleteComment);
export const deleteComment = asyncHandler(async (req, res) => {
const post = await Post.findById(req.params.id);
const comment = post.comments.find(
(comment) => comment._id === req.params.comment_id
);
if (!comment) {
res.status(404);
throw new Error('Comment not found');
}
//Check User
if (comment.user.toString() === req.user._id.toString()) {
post.comments = post.comments.filter(
({ id }) => id !== req.params.comment_id
);
await post.save();
return res.json(post.comments);
} else {
res.status(401);
throw new Error('User not authorized');
}
});
这里是 post 模型
import mongoose from 'mongoose';
const postSchema = mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: [true, 'Please Author is required'],
},
title: {
type: String,
required: true,
},
desc: {
type: String,
required: true,
},
img: {
type: String,
},
isLiked: {
type: Boolean,
default: false,
},
isDisLiked: {
type: Boolean,
default: false,
},
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
disLikes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
comments: [
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
text: {
type: String,
required: true,
},
name: {
type: String,
},
pic: {
type: String,
},
date: {
type: Date,
default: Date.now,
},
},
],
categories: {
type: Array,
},
},
{
timestamps: { createdAt: 'created_at', updatedAt: 'modified_at' },
}
);
const Post = mongoose.model('Post', postSchema);
export default Post;
当您访问 _id
时,您正在访问 ObjectId
您应该尝试与 id
进行比较,这是 _id
const comment = post.comments.find(
(comment) => comment.id === req.params.comment_id
);