导出包含mongoose模式的模块

时间:2017-09-22 13:12:15

标签: node.js mongodb mongoose

这是模块部分:     DOC / docRdv

module.exports = function (mongooseRdv){
   var RdvSchema = new Schema({
     Date: {type: Date},
   });

   var models ={
     Rdv : mongoose.model('Rdv',RdvSchema)
   };

   return models;
}    

这是main.js:

var mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1/DocTest');

var docRdv = require('./doc/docRdv');

var Rdv1 = new models.Rdv({
   Date: new Date
});

我收到以下错误:

ReferenceError: models is not defined
at Object.<anonymous> (c:\DOCFLEET\Test\main.js:5:16)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Timeout.Module.runMain [as _onTimeout] (module.js:604:10)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)

任何人都知道如何修复它?

1 个答案:

答案 0 :(得分:0)

您正在导出一个创建models而不是models的函数。

new docRdv().Rdv(...)

中使用main

或执行该功能并导出结果

module.exports = (function (mongooseRdv){
   var RdvSchema = new Schema({
     Date: {type: Date},
   });

   var models ={
     Rdv : mongoose.model('Rdv',RdvSchema)
   };

   return models;
})();