【问题标题】:Is it possible to have an embedded external mongoose schema in another mongoose schema?是否有可能在另一个猫鼬模式中嵌入一个外部猫鼬模式?
【发布时间】:2020-12-16 12:46:17
【问题描述】:

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

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);

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    是的,
    你只需要更换

    comments: [ Comment.commentSchema ]
    

    comments: [ Comment.schema ]
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2019-03-22
      • 2016-08-05
      • 2021-08-17
      • 2013-03-12
      • 2012-02-02
      • 2020-09-18
      相关资源
      最近更新 更多