我剛剛開始學習 MERN 堆疊,但在使用 Express/Node 更新模型中的文本時遇到問題。我試圖尋求幫助并訪問
uj5u.com熱心網友回復:
獲取評論后,您應該更新 cat 實體。
嘗試像這樣更改您的代碼(使用async wait):
const updateComment = async (req, res) => {
// if there is no req.body, return error
if (!req.body) {
return res.status(400).json({
success: false,
error: 'You must provide a body to update',
});
}
try {
// req.body exists, so find the comment by id and then update
const comment = await Comment.findById(req.params.id);
if (!comment) {
return res.status(404).json({
err,
message: 'Comment not found!',
});
}
// update the comment details
comment.text = req.body.text;
// save the updated comment
await comment.save();
// now update the comment entry for the cat too
const cat = await Cat.findById(comment.cat_id);
const otherCatComments = cat.comments.filter((c) => c._id !== comment._id);
cat.comments = [...otherCatComments, comment];
await cat.save();
res.status(200).json({
success: true,
id: comment._id,
message: 'Comment updated!',
});
} catch (err) {
res.status(404).json({
error,
message: 'Comment not updated!',
});
}
};
uj5u.com熱心網友回復:
盧卡,謝謝!這非常有幫助,我可以看到添加到貓評論陣列的附加評論。現在唯一的問題是cats.comment.filter 沒有按預期作業,因為otherCatsComments 仍然包含所有評論。我不得不在代碼中做一些挖掘,我設法控制臺記錄了 id,它回傳“_id:new ObjectId(“617d57719e815e39f6049452”),”我嘗試將其更改為
const otherCatComments = cat.comments.filter((c) => c._id !== `new ObjectId("${comment._id}")`);
const otherCatComments = cat.comments.filter((c) => c._id !== ` new ObjectId("${comment._id}")`);
const otherCatComments = cat.comments.filter((c) => c._id !== `ObjectId("${comment._id}")`);
但是它們似乎都不起作用,所以我不得不進行深入除錯,結果發現我的代碼在某些情況下已關閉!我只會在此處添加它們,以防將來有人遇到此問題。首先,我的評論 ID 與我的貓模型中的評論 ID 不同。作為參考,這是我的創建評論模型(我將其修改為使用 Luca 推薦的 async/await try/catch 塊:
const createComment = async (req, res) => {
// if there is no req.body, return error
if (!req.body) {
return res.status(400).json({
success: false,
error: "You must provide a comment",
});
}
try {
// req.body exists, so make a new comment
const comment = new Comment(req.body);
await comment.save();
// now add comment to cat
Cat.findById(req.params.id, (err, foundCat) => {
// Append the comment to the cat
foundCat.comments.push(comment);
foundCat.save();
});
// somehow, if the new comment doesn't exist, return error
if (!comment) {
return res.status(400).json({ success: false, error: err });
}
// success!
res.status(201).json({
success: true,
id: comment._id,
message: "Comment created!",
});
} catch (err) {
return res.status(400).json({
err,
message: "Comment not created!",
});
}
};
請注意我在貓中添加評論的部分:起初是
foundCat.comments.push(req.body);
但這會在 cat 中生成一個與評論中的評論 ID 不同的評論 ID。所以 req.body 改為評論。
修復后,我嘗試了 Luca 的原始代碼,但仍然無法正常作業。我的解決方法是不使用過濾器,只洗掉舊評論,然后添加新評論。代碼在這里:
const updateComment = async (req, res) => {
// if there is no req.body, return error
if (!req.body) {
return res.status(400).json({
success: false,
error: "You must provide a body to update",
});
}
try {
// req.body exists, so find the comment by id and then update
const comment = await Comment.findById(req.params.id);
if (!comment) {
return res.status(404).json({
err,
message: "Comment not found!",
});
}
// update the comment details
comment.text = req.body.text;
// save the updated comment
await comment.save();
// now update the comment entry for the cat too
const cat = await Cat.findById(comment.cat_id);
// remove the old, non-updated comment first
cat.comments.id(comment._id).remove();
// now add in the updated comment
cat.comments.push(comment);
await cat.save();
res.status(200).json({
success: true,
id: comment._id,
message: "Comment updated!",
});
} catch (err) {
res.status(404).json({
error,
message: "Comment not updated!",
});
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343592.html
上一篇:簡化MongoDB聚合
