[英]How to nest schemas in mongoose?
我正在嘗試使用貓鼬嵌套模式,但是我被卡住了,我真的不知道為什么。 這就是我得到的。
我的父架構
const Comment = require("./Comment");
const BookSchema = new Schema({
_id: Number,
comments: [{ comment: Comment }],
ratings: [{ rate: Number }],
calculatedRating: Number
});
module.exports = Book = mongoose.model("book", BookSchema);
和子模式
const CommentSchema = new Schema(
{
userName: String,
rating: Number,
body: String,
submit_date: {
type: Date,
default: Date.now
}
},
{ _id: false }
);
module.exports = Comment = mongoose.model("comment", CommentSchema);
並通過此設置即時得到一個錯誤:
“ TypeError:無效的架構配置:在路徑注釋處,模型不是有效的類型。”
我正在考慮我對那些出口做錯了,但我不確定。
您的./評論應為:
const CommentSchema = new Schema(
{
userName: String,
rating: Number,
body: String,
submit_date: {
type: Date,
default: Date.now
}
},
{ _id: false }
);
module.exports = CommentSchema;
如果您像定義的那樣定義為新模型,則它將創建自己的集合,並且將是新模型,而不是子文檔架構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.