保存时从未达到过Mongoose回调

时间:2015-04-09 12:21:42

标签: node.js callback mongoose save

由于某种原因,当尝试使用Mongoose将数据保存到MongoDB数据库时,永远不会调用回调函数。首先我认为没有触发错误代码,但后来我测试了是否完全输入了回调函数,但事实并非如此!并且数据库中没有保存任何内容。我甚至无法在存储其他MongoDB数据库的文件夹中看到名为client-db的文件(通过命令行) - 这可能是问题吗?

我尝试过类似问题的不同解决方案,但到目前为止还没有任何工作。有没有人有任何想法我做错了什么?我提供任何帮助!

以下是我的代码的相关部分:

客户db.js:

var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/client-db', function(){
  console.log("Client DB: connected"); // This is shown
});

module.exports = mongoose;
//module.exports = db; // Another problem was solved by this, but didn't work for me

积data.js:

var db = require('client-db');

var plotDataSchema = db.Schema({
  time: {type: Number, required: true},
  value: {type: Number}
});

var PlotData = db.model('PlotData', plotDataSchema);

module.exports = PlotData;

controller.js:

var PlotData = require('plot-data');

//data is an object with time and value

var plotData = new PlotData({
  time: data.time,
  value: data.value
});

plotData.save(function(err){
  console.log('Entered callback'); // Never shown
  if(err){
    console.log(err); // Never shown
    //return handleErrror(err); // Different things I tried when I thought the error handling was the problem - didn't change anything
    //return next(err);
  }else{
    console.log('Plot data saved in client db'); // Never shown
  }
});

1 个答案:

答案 0 :(得分:2)

如果您确实已连接,那么看起来您正在使用数据库连接和保存操作进行竞争。将代码压缩到单个文件并在连接回调中执行保存工作。

var mongoose = require('mongoose');
var plotDataSchema = mongoose.Schema({
  time: {type: Number, required: true},
  value: {type: Number}
});
var PlotData = mongoose.model('PlotData', plotDataSchema);
var plotData = PlotData({
  time: 1234,
  value: 5678
});

mongoose.connect('mongodb://localhost/client-db', function(err) {
  if (err) { console.log(err); return; }
  console.log("Client DB: connected"); // This is shown

  plotData.save(function(err, plot){
    console.log(err, plot);
    // outputs
    // null { __v: 0, time: 1234, value: 5678, _id: 5526892bcffa7a0e973fb456 }
  });
});

还有一些关于猫鼬的一般说明:

  • 它具有模型,模式和连接的内部全局缓存(以及每个连接的缓存),因此无需执行此操作:module.exports = mongoose;。只要你需要它就需要mongoose(节点也有自己的缓存,所以它只会将已经存在的mongoose导出到请求文件)。
  • 关注以上几点,由于模型的内部缓存,您不需要这样做:module.exports = PlotData。只需要mongoose,然后你可以执行:var PlotData = mongoose.model('PlotData');从缓存中检索模型。

所以你的文件看起来像是:

<强>客户db.js

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/client-db', function(err){
  if (err) { console.log(err); return; }
  console.log("Client DB: connected"); // This is shown
});

<强>积data.js

var mongoose = require('mongoose');
var plotDataSchema = mongoose.Schema({
  time: {type: Number, required: true},
  value: {type: Number}
});

mongoose.model('PlotData', plotDataSchema);

<强> controller.js

var mongoose = require('mongoose');
var PlotData = mongoose.model('PlotData'); // Notice schema is not specified

var plotData = new PlotData({
  time: data.time,
  value: data.value
});

// NEED TO WAIT FOR CONNECTION BEFORE CALLING
plotData.save(function(err, plot){
  console.log(err, plot);
});
相关问题