是否可以在另一个猫鼬模式中嵌入嵌入式外部猫鼬模式?

时间:2020-08-27 15:26:14

标签: node.js mongodb mongoose mongoose-schema

是否可以制作一组外部猫鼬模式? 当我这样运行时,控制台会说:

TypeError: Invalid value for schema Array path `comments`, got value "undefined"

comment.js

const mongoose = require('mongoose');

const commentScheme = new mongoose.Schema({
    body: String,
    author: {
        id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
        username: String
    }
});

module.exports = mongoose.model('Comment', commentScheme);

campground.js

const mongoose = require('mongoose'),
    Comment = require('./comment');

const campgroundSchema = new mongoose.Schema({
    name: String,
    image: String,
    desc: String,
    comments: [ Comment.commentSchema ]
});

module.exports = mongoose.model('Campground', campgroundSchema);

1 个答案:

答案 0 :(得分:0)

是的,
您只需要更换

comments: [ Comment.commentSchema ]

使用

comments: [ Comment.schema ]

祝你好运!