【问题标题】:How to create interdependent schemas in Mongoose?如何在 Mongoose 中创建相互依赖的模式?
【发布时间】:2012-08-19 11:37:03
【问题描述】:

我有两个 Schema,我希望它们相互交互。例如:

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

但是,我收到错误消息,因为每个架构都依赖于另一个架构。有没有办法使用猫鼬来完成这项工作?我像这样包含它们:

// app.js
require('./models/calendar');
require('./models/day');

【问题讨论】:

    标签: node.js mongoose database-schema


    【解决方案1】:

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

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

    在 day.js 中做同样的事情

    编辑:正如 JohnnyHK 所说,这不起作用。 Link to explanation

    【讨论】:

    • 我不认为这将取决于您所需文件的顺序,因为 circular dependency,calendar.js 或 day.js 只会看到另一个的部分版本。
    【解决方案2】:

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

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

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

    真的就是这么简单。 Brian Bickerton 的解释可以在这里找到:

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

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

    【讨论】:

    • 天哪,这看起来很糟糕。这不会引起任何问题吗?您似乎只导出了部分架构;下面的东西呢?其他东西可以导入架构甚至看到 mongoose.exports 行下的内容吗?编辑:这甚至不起作用。抱怨意外的令牌,这非常有意义。
    • 效果很好,自己试试吧。比我更熟悉节点的人可能会解释原因。
    • 我正在尝试。在一个文件中,我有:puu.sh/k2EsP/d938a37098.png。在另一个我有:puu.sh/k2EtB/d9f08d02f5.png。但是我仍然收到错误消息,在后一个文件的相应行中,没有为模型“Item”注册 Schema。
    • 您正在使用不同的方法包含其他架构。尝试使用 var ItemSchema = require("../path/to/item.js") 而不是 Mongoose.model('Item')
    • 这行得通,尽管我不知道这两者之间的区别是什么。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2022-01-16
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2017-04-08
    相关资源
    最近更新 更多