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);
然后只需根據需要填充“響應”字段。

TA貢獻1789條經驗 獲得超10個贊
在您的頂部代碼段中,您有responses: [CommentSchema]
但 CommentSchema 仍未定義,因為此代碼段正在定義它。您不能進行這種遞歸定義。
添加回答
舉報