mongoose没有开火拯救回调

时间:2015-04-30 16:27:35

标签: node.js mongodb database-connection

我很困惑。 已安装的mongoose-module(最新版本)未触发保存回调。

我定义了以下架构:

var Object = new global.db.Schema({
  customerId: { type: String, required: true },
  customerPrefix: { type: String, required: true },
  name: { type: String, required: true },
  fields: { type: Array, default: [] },
  // ... (other fields have default values) 
});

global.db是mongoose(server.js):

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/dbName');
var connection = mongoose.connection;
connection.on('error', function(dbError) {
    console.log(dbError); // no errors happend 
});

connection.once('open', function() {
    global.db = mongoose;
    global.models = require('./lib/dao');
});

global.models包含模型(lib / dao /)

module.exports = global.db.model('Object', dbObjectSchema);

现在我尝试创建一个新文档:

var newObject = new global.models.Object({});
newObject.customerId = data.customerId;
newObject.customerPrefix = data.prefix;
newObject.fields = data.fields;
newObject.name = data.name;

console.log(global.db.connection.host); // localhost
console.log(global.db.connection.port); // 27017
// i've tested also whether i can call .find (it works and callback will fired!)
newObject.save(function(err) {
   // this part will never called
   console.log("save callback fired"); 
   if (typeof callback == 'function') {
      callback(err, newObject);
   }
});

我绝对不明白为什么回调没有被解雇。 测试一些已知问题(例如,连接未建立,......) 我已经插入了一些调试输出,但一切看起来都很好:(

任何人都可以帮助我吗? :/

祝你好运

0 个答案:

没有答案