如何在Mongoose中创建相互依赖的模式?

Rob*_*tin 9 mongoose database-schema node.js

我有两个模式,我希望他们互相交流.例如:

// calendar.js
var mongoose = require('mongoose');
var Scema = mongoose.Schema;
var Day = mongoose.model('Day');

var CalendarSchema = new Schema({
  name: { type: String, required: true },
  startYear: { type: Number, required: true }
});

CalendarSchema.methods.getDays = function(cb){
   Day.find({ cal: this._id }, cb);
}

module.exports = mongoose.model('Calendar', CalendarSchema);


// day.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Calendar = mongoose.model('Calendar');    

var DaySchema = new Schema({
  cal: { type: ObjectId, required: true },
  date: { type: Number, required: true },
  text: { type: String, default: 'hello' }
});

DaySchema.methods.getCal = function(cb){
   Calendar.findById(this.cal, cb);
}

module.exports = mongoose.model('Day', DaySchema);   
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误,因为每个架构都依赖于另一个架构.有没有办法让这个使用Mongoose工作?我把它们包括在内:

// app.js
require('./models/calendar');
require('./models/day');
Run Code Online (Sandbox Code Playgroud)

mz3*_*mz3 5

我意识到这是一个古老的线程,但我相信发布解决方案会帮助其他人。

解决方案是在需要相互依赖的模式之前导出模块:

// calendar.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CalendarSchema = new Schema({
  name: { type: String, required: true },
  startYear: { type: Number, required: true }
});

module.exports = mongoose.model('Calendar', CalendarSchema);

// now you can include the other model and finish definining calendar
var Day = mongoose.require('./day');    
CalendarSchema.methods.getDays = function(cb){
   Day.find({ cal: this._id }, cb);
}


// day.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;   

var DaySchema = new Schema({
  cal: { type: ObjectId, required: true },
  date: { type: Number, required: true },
  text: { type: String, default: 'hello' }
});

module.exports = mongoose.model('Day', DaySchema);

// same thing here. require after exporting
var Calendar = require('./calendar'); 

DaySchema.methods.getCal = function(cb){
   Calendar.findById(this.cal, cb);
}
Run Code Online (Sandbox Code Playgroud)

真的就是这么简单。可以在此处找到布赖恩·比克顿 (Brian Bickerton) 的解释:

http://tauzero.roughdraft.io/3948969265a2a427cf83-requiring-interdependent-node-js-modules

能够在模块中按名称使用函数而不是冗长的 module.exports.name 是很好的。有一个地方可以查看和查看要导出的所有内容,这也很好。通常,我看到的解决方案是正常定义函数和变量,然后将 module.exports 设置为最后包含所需属性的对象。这在大多数情况下都有效。当两个模块相互依赖并相互需要时,它就会崩溃。最后设置导出会导致意外结果。要解决这个问题,只需在顶部指定 module.exports,然后再需要其他模块。


Don*_*pez 0

您需要需要这些文件。如果它们位于同一路径中,请执行以下操作:

//calendar.js
var Day = require('./day');
/* Other logic here */
var CalendarSchema = new Schema({
  name: { type: String, required: true },
  startYear: { type: Number, required: true }
})
, Calendar;

/* other logic here */
/* Export calendar Schema */

mongoose.model('Calendar', CalendarSchema);
Calendar = mongoose.model('Calendar');
exports = Calendar;
Run Code Online (Sandbox Code Playgroud)

在 day.js 中执行相同操作

编辑:正如 JohnnyHK 所说,这不起作用。链接到解释

  • 我认为这不会根据所需文件的顺序起作用,由于[循环依赖](http://nodejs.org,calendar.js 或 day.js 只会看到另一个的部分版本/api/modules.html#modules_cycles)。 (2认同)