简体   繁体   English

RestMVC.js(Mongoose)和Node.JS-外部模型文件

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

I am using RestMVC.js module that uses Mongoose in turn for model declaration. 我正在使用RestMVC.js模块,该模块依次使用Mongoose进行模型声明。 Let's say I have a few files with model declarations, and a few of them use those classes for member variable ala: 假设我有一些带有模型声明的文件,其中一些将这些类用于成员变量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. 我试图做require('models / Foo.js')和export.Foo以及mongoose.exports.Foo无济于事。

Best approach is 最好的方法是

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

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

应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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