嵌入架构给了错误

嵌入架构给了错误

本文介绍了嵌入架构给了错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将模式嵌入到我创建的其他模式中,并且不断出现此错误:

I am trying to embed schemas in other schemas that I have created, and I keep getting this error:

我不完全确定这里出了什么问题,但是我想做的是将对我的事件模式和兴趣模式的引用存储在用户模式中.如果有人可以告诉我我在做什么错,那将非常感谢!

I am not entirely sure what is going wrong here, but what I want to do is store references to my event schema and interests schema inside the user schema. If someone could tell me what I'm doing wrong that would be great thanks!

我现在收到一个新错误:

   /Users/Dynee/node_modules/mongoose/lib/schema.js:421
  throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`');
  ^

TypeError: Invalid value for schema Array path `eventsHosted`
at Schema.add (/Users/Dynee/node_modules/mongoose/lib/schema.js:421:13)
at new Schema (/Users/Dynee/node_modules/mongoose/lib/schema.js:99:10)
at Object.<anonymous> (/Users/Dynee/Documents/eventure-rest-backend/Models/User.js:5:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/Dynee/Documents/eventure-rest-backend/Models/Event.js:2:43)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)

我的用户架构

var mongoose = require('mongoose');
var EventSchema = require('../Models/Event').schema;
var InterestSchema = require('../Models/Interest').schema;

var UserSchema = new mongoose.Schema({
    email: String,
    password: String,
    eventsHosted: [EventSchema],
    eventsAttended: [EventSchema],
    currentlyAttending: [EventSchema],
    currentlyHosting: [EventSchema],
    profileImage: String,
    interests: [InterestSchema],
    followers: [UserSchema],
    following: [UserSchema]

});

module.exports = mongoose.model('User', UserSchema);

我的事件架构

var mongoose = require('mongoose');
var UserSchema = require('../Models/User').schema;

var EventSchema = new mongoose.Schema({
    title: String,
    description: String,
    location: String,
    attendees: [UserSchema],
    date: String,
});

module.exports = mongoose.model('Event', EventSchema);

我的兴趣模式

var mongoose = require('mongoose');

var InterestSchema = new mongoose.Schema({
    name: String
});

module.exports = mongoose.model('Interest', InterestSchema);

推荐答案

这是因为在模块中导出的不是Schemas,而是Models.当您执行var EventSchema = require('../Models/Event');时,您需要的是Event Model,而不是Event Schema.要从模型访问基础架构,您可以执行以下操作:

It's because what you are exporting in your modules aren't Schemas, they are Models. When you do var EventSchema = require('../Models/Event'); you are requiring the Event Model, not the Event Schema. To access the underlying schemas from your models you can do:

var EventSchema = require('../Models/Event').schema;
var InterestSchema = require('../Models/Interest').schema;
var UserSchema = require('../Models/User').schema;

当您引用另一个集合的文档时,您也会遇到问题,这应该可行:

You also have a problem when you are referencing documents that belongs to another collection, this should work:

事件模型:

var mongoose = require('mongoose');
var UserSchema = require('./User').schema;

var EventSchema = new mongoose.Schema({
    title: String,
    description: String,
    location: String,
    attendees: [{ type: Schema.Types.ObjectId, ref: 'User' }],
    date: String
});

module.exports = mongoose.model('Event', EventSchema);

兴趣模型:

var mongoose = require('mongoose');

var InterestSchema = new mongoose.Schema({
    name: String
});

module.exports = mongoose.model('Interest', InterestSchema);

用户模型:

var mongoose = require('mongoose');
var EventSchema = require('./Event').schema;
var InterestSchema = require('./Interest').schema;

var UserSchema = new mongoose.Schema({
    email: String,
    password: String,
    eventsHosted: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    currentlyAttending: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    currentlyHosting: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    profileImage: String,
    interests: [{ type: Schema.Types.ObjectId, ref: 'Interest' }],
    followers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
    following: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});

module.exports = mongoose.model('User', UserSchema);

如何在我的Mongoose模式中引用另一个模式? /a> http://mongoosejs.com/docs/populate.html

How to reference another schema in my Mongoose schema?http://mongoosejs.com/docs/populate.html

这篇关于嵌入架构给了错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:12