【问题标题】:Create embeded docs mongoose创建嵌入式文档 mongoose
【发布时间】:2017-06-22 16:02:05
【问题描述】:

我有以下猫鼬模式:

'use strict';

const PostSchema = new Schema({
    description: require('../fields/description'),
    image: require('../fields/image'),
    createdAt: require('../fields/createdAt'),
    location: Location,
    owner: require('../fields/owner') }, {
    toObject: {
        virtuals: true,
        getters: true,
        setters: true
    },
    toJSON: {
        virtuals: true,
        getters: true,
        setters: true
    } });

module.exports = mongoose.model('Post', PostSchema);

LocationSchema.js

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const LocationSchema = new Schema({
    description: {
    type: String,
    required: true
},
    point:{
    type: [Number],
    index: '2d',
    required: true
},
    createdAt:{
    type: Date,
    required: true,
    default: new Date()
}
});

module.exports = mongoose.model('Location', LocationSchema);

当我重新启动我的 nodejs 服务器时,我收到以下错误消息:

TypeError: Undefined type Model at location 你尝试过嵌套吗 模式?您只能使用 refs 或数组进行嵌套。

有人知道怎么解决吗?

【问题讨论】:

  • 您能否在此处添加更多详细信息。关于Location 是什么。
  • 我添加 LocationSchema
  • 这是您的问题吗 => 您的帖子集合有一个位置键,这些位置保存在不同的集合中,称为位置?

标签: node.js mongodb mongoose mongoose-schema


【解决方案1】:

您的错误表示您没有在您的帖子集合中提供位置键的引用,如果您想为您的帖子集合提供位置 ID,您必须提供对您的集合的引用。

示例:

var PostSchema = new mongoose.Schema({
  location: [{ type: mongoose.Schema.Types.ObjectId, ref: 'location' }]
});

【讨论】:

    猜你喜欢
    • 2013-06-29
    • 2011-12-21
    • 2014-07-13
    • 2016-09-28
    • 1970-01-01
    • 2018-03-30
    • 2015-10-14
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多