所以我遇到了一個問題,試圖向我的 mongoDB 資料庫中已經存在的一些資料添加注釋。我想這樣做,以便我可以為保存在我的資料庫中的每個帳戶添加、洗掉和更新評論,但我不確定我做錯了什么。我已經設定了我的前端來發送一個新的評論和所有需要隨之而來的資料。它成功地到達我的后端,在我的后端它說它通過并被保存但它沒有,當我重新加載頁面時,我的資料中的評論陣列仍然為0。
Account.findByIdAndUpdate(
{ _id: comment.accountId },
{
$push: {Account: { comments: comment }},
},
{ new: true, upsert: true }
).exec(function (err, task) {
if (err) {
return res
.status(400)
.send({ message: "Failed to add comment due to invalid params" });
}
console.log("successfully uploaded comment");
return res.status(200).send(task);
});
所以在這里我們正在加載特定的帳戶,然后將該新評論推送到我的貓鼬模式中的評論陣列。當我取出“帳戶:物件并僅使用評論:評論對物件進行處理時,它說后端存在內部錯誤,我猜沒有找到引數,這將是評論陣列。
我不知道為什么這會是一個問題。下面也是我的貓鼬模式。
const AccountSchema = new Schema({
username: {
type: String,
},
name: {
type: String,
},
date: {
type: Date,
default: Date.now,
},
description: {
type: String,
},
latitude: {
type: Number,
},
longitude: {
type: Number,
},
comments: [
{
id: Number,
text: String,
type: String,
date: Date,
replies: [{ id: Number, text: String, type: String, date: Date }],
},
],
});
我沒有正確設定我的架構嗎?還是我忘記在某處添加一些東西。任何幫助都會很棒,謝謝!
uj5u.com熱心網友回復:
看起來你的更新查詢的$push規范是罪魁禍首。在代碼中,它說:
$push: {Account: { comments: comment }}
但是您的Account模型沒有將物件推入的“帳戶”欄位。要將新元素插入到“comments”陣列中,請改為執行以下操作:
$push: { comments: comment }
uj5u.com熱心網友回復:
只是想發布一個更新,我改變了 findbyidandupdate 上的選項,我添加了 new 和 safe 并將 upsert 全部為 true,然后我意識到我的主要問題是我的架構中的評論陣列設定為型別:字串,而不是陣列。大聲笑感謝幫助家伙。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/354902.html
上一篇:ReactJS使用路徑匯入SVG
