【问题标题】:RestMVC.js(Mongoose) & Node.JS - External model filesRest MVC.js(Mongoose) 和 Node.JS - 外部模型文件
【发布时间】:2011-10-31 19:10:05
【问题描述】:

我正在使用 RestMVC.js 模块,该模块依次使用 Mongoose 进行模型声明。假设我有一些带有模型声明的文件,其中一些将这些类用于成员变量 ala:

// Foo.js file
module.exports.Foo = function(mongoose)
{
  var Schema = mongoose.Schema;

  var Foo = mongoose.model('Foo', new Schema({
   TestMember: String,
   SecondTestMember: Date
  }));

  return mongoose.model('Foo');
};

// Bar.js file
module.exports.Bar = function(mongoose)
{
   var Schema = mongoose.Schema;

   var Bar = mongoose.model('Bar', new Schema({
   DerivedMember: Foo,
   Blah: String
  }));

  return mongoose.model('Bar');
};

从一个模型中引用另一个模型的正确方法是什么?我试图做 require('models/Foo.js') 和 exports.Foo 以及 mongoose.exports.Foo 无济于事。

【问题讨论】:

  • 一种方法是预加载所有模式,但存储和访问它们的最佳位置是什么?我不想污染全局空间,也许是 mongoose.Schemas 数组,有人吗?

标签: javascript model-view-controller mongodb node.js mongoose


【解决方案1】:

最好的方法是

var ASchema = new Schema({
        BObj: {type: ObjectID, ref: 'B'},
        Amount: Number,
        Timestamp: Date
        });

【讨论】:

    【解决方案2】:

    require("models/Foo.js").Foo

    应该可以工作

    【讨论】:

    猜你喜欢
    • 2016-04-30
    • 2017-08-11
    • 2018-01-30
    • 2020-12-21
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 2014-11-02
    • 2012-12-13
    相关资源
    最近更新 更多