亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用貓鼬嵌套模式?

如何使用貓鼬嵌套模式?

www說 2022-12-22 14:41:14
const CommentSchema = new mongoose.Schema({    username: {        type: String,        required: true,    },    detail: {        type: String,        required: true,    },    responses: [CommentSchema]})const PostSchema = new mongoose.Schema({    author: {        type: String,        required: true,    },    title: {        type: String,        required: true    },    comments: [CommentSchema]})我不斷收到 CommentSchema 不存在的引用錯誤。如何使用 mongoose 嵌套模式?我認為錯誤是因為在 const commentschema 中調用了 commentSchema。我以前見過這個,所以我不知道它是否可能
查看完整描述

2 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

您是否嘗試在創建架構后添加“響應”字段?


const CommentSchema = new mongoose.Schema({

    username: {

        type: String,

        required: true,

    },


    detail: {

        type: String,

        required: true,

    },

});

CommentSchema.add({ responses: [CommentSchema] });

不過,我可能會這樣做的方式是保留您的原始設置并將響應保存為評論模型的 ObjectId。


const CommentSchema = new mongoose.Schema({

    username: {

        type: String,

        required: true,

    },


    detail: {

        type: String,

        required: true,

    },

    responses: [{ type: ObjectId, ref: 'Comment' }],

});


const Comment = model('Comment', CommentSchema);

然后只需根據需要填充“響應”字段。


查看完整回答
反對 回復 2022-12-22
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

在您的頂部代碼段中,您有responses: [CommentSchema]但 CommentSchema 仍未定義,因為此代碼段正在定義它。您不能進行這種遞歸定義。



查看完整回答
反對 回復 2022-12-22
  • 2 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號