Mongoose - TypeError:Object不是构造函数

时间:2017-04-20 22:27:11

标签: node.js mongodb mongoose

由于其他解决方案对我无效:

mongoose / connection.js (控制台正在打印 - Mongo DB已连接。)

var mongoose = require('mongoose');

var url = process.env.URL,
options = {
  user: process.env.DBUSER,
  pass: process.env.DBPASSWORD
};

var connection = mongoose.createConnection(url, options);

connection.on('error', function(err){
  if(err) throw err;
});

connection.once('open', function callback () {
  console.info('- - Mongo DB connected .');
});

module.exports = connection;

mongoose / model.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var Event = new Schema({
   event_code: String
});

var Model = mongoose.model('Event', Event);
module.export = Model;

猫鼬/ database.js

function newLiveEvent (event_code, callback) {

  var LiveEvent = require ('./model.js');

  var newEvent = new LiveEvent({
     event_code: event_code
  });

  newEvent.save(function(err) {
     if (err) throw err;
     console.log('Event saved !')
  });

  callback();

}

问题一个简单的表单使用event_code发送一个post请求,并不简单。

这是newLiveEvent函数被触发时得到的... TypeError: LiveEvent is not a constructor

还有其他几个问题,遗憾的是他们的解决方案对我不起作用,任何想法?

1 个答案:

答案 0 :(得分:1)

在数据库文件中,您应该要求模型,连接,最后创建LiveEvent作为connection.model('Event'),以便能够调用save方法。