简体   繁体   中英

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 model 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

应该工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM