我已經在這上面呆了好幾天了。我已經嘗試了很多東西。我知道我做得不對。我有一個博客應用程式,我正在使用node.js、mongodb和react.js構建。所以,到目前為止,我有用戶模型、帖子模型和評論模型。我把它們連接起來。我對帖子和評論模型所做的是這樣的。當一個發表評論的用戶洗掉評論時,我希望Post集合中該評論的參考id也能從資料庫中洗掉。如果評論的參考ID仍然在資料庫中,即使它沒有內容,也是沒有意義的。當你有很多被洗掉的評論時,這可能會很混亂。評論模型被參考到帖子模型中。因此,當評論集合中的評論被洗掉時,帖子集合中參考的id也應該被洗掉。看看我到目前為止的代碼:
評論模型
評論模型 我在這個網站上看到一個帖子,說是用Mongodb中間件來做這件事。我在我的評論模型中應用了中間件的代碼,如你所見。它沒有作業。在洗掉一個評論后,在Post集合中參考的id仍在陣列中。我確信我的做法是不正確的。
帖子模型 評論洗掉代碼 獲取職位代碼 這是我所說的參考的id的截圖。這個特定的評論已經被洗掉了,但是這個ID仍然可以在發表評論的那個特定帖子陣列中找到。
uj5u.com熱心網友回復: 你的代碼將有點像這樣: 你的代碼將有點像這樣: 因此,你的代碼也會有點像上面的代碼,我已經寫好了,供你參考。
uj5u.com熱心網友回復: 所以用這個來代替 : 嘗試使用.pre('remove')或.pre('delete')而不是update
標籤: 下一篇:來自RESTfulAPI的post請求給出了UnhandledPromiseRejectionWarning。驗證錯誤。產品驗證失敗:錯誤
const mongoose = require("mongoose")。//import mongoose to be used.
const Schema = mongoose.Schema;
const CommentSchema = new mongoose.Schema(
{
commentdescription:{
type: String,
required: true,
},
author:{
type: Schema.Types.ObjectId。
ref。'User'。
},
postId:{
type: Schema.Types.ObjectId。
ref。"Post"。
}
}, {timestamps: true}.
);
CommentSchema.pre('update',function(next) {
this.model('Post').update(
{ },
{ "$pull": { "comments"。this._id }。},
{ "multi": true },
下一個
);
})
//exporting this schema: true }; }.
module.exports = mongoose. model("Comment", CommentSchema); //the module name is "Post"。
//creating the user models for the database<
const mongoose = require("mongoose"); //import mongoose。
const Schema = mongoose.Schema;
const PostSchema = new mongoose.Schema(
{
title:{
type: String,
required: true,
unique: true。
},
description:{
type: String,
required: true,
},
postPhoto:{
type: String,
required:false,
},
username:{
type: Schema.Types.ObjectId。
ref。'User'。
},
categories:{
type: Array,
},
comments: [{
type: mongoose.Schema.Types.ObjectId。
ref。'Comment'。
unique: true。
}]
}, {timestamps: true},
);
//exporting this schema
module.exports = mongoose. model("Post", PostSchema); //the module name is "Post"。
router.delete("/posts/:id/comment/:id"/span>, async (req, res) =>{
try{
const comment = await Comment. findById(req.params.id) 。
if(comment.author == req.body.author){
try{
await comment.delete()
res.status(200).json("注釋已被洗掉")
}catch(err){
console.log(err)
}
}
else{
res.status(401).json("you can only delete your posts" )
}
}catch(err){
console.log(err)
}
})
/Get Post。
router.get("/:id"/span>, async(req, res)=>{
try{
const post = await Post.findById(req. params.id).populate('username').populate({
path: "comments",
populate: {
path: "author",
}
})
span class="hljs-attr">deleteCommentById: async function(req, res) {
try {
if (req.params.type == "Comment") {
await postModel.updateMany({ postId: ObjectId(req.params.postId), type: 'post' }, { $pull: { 'children': ObjectId(req.params.id) } });
await commentModel.updateMany({ postId: ObjectId(req.params.postId) }, { $pull: { 'comments': ObjectId(req.params.id) } });
}
await postModel.findByIdAndDelete(ObjectId(req. params.id))。)
return res.status(http_status.OK).json({ message。`${req.params.type} 成功洗掉` })。
} catch (錯誤) {
return res.send("error") 。
}
}
CommentSchema。 pre('update'/span>,function(next) {

