开发者

RestMVC.js(Mongoose) & Node.JS - External model files

I am using RestMVC.js module that uses Mongoose in turn for model declaration. Let's say I have a few files with model declarations, and a few of them use those classes for member variable 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');
};

What is the correct approach to reference one 开发者_如何学Pythonmodel from the other? I attempted to do require('models/Foo.js') and exports.Foo as well as mongoose.exports.Foo to no avail.


Best approach is

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


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

Should just work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜