node.js表示mongodb和尚插入 - 没有消息的错误

时间:2016-02-28 20:19:26

标签: node.js mongodb express monk

我有这段代码

/* GET */
router.get('/mycol', function(req, res) {
  var db = req.db;
  var collection = db.get('mycol');
  collection.find({}, {}, function(err, docs) {
    res.json(docs);
  });
});

/* POST */
router.post('/mycol', function(req, res) {

  var db = req.db;
  var collection = db.get('mycol');

  collection.insert({"test":"testing"}, function(err, docs) {
    if (err) {
      res.json({"error":err});
    }
    else {
      res.json(docs);
    }
  });

});

GET工作正常,但POST没有 - 插入返回错误,错误json只是:

{"error":{"name":"MongoError"}}

如果我将它输入mongo控制台,完全相同的插入工作正常:

> db.mycol.insert({"test":"testing"})
WriteResult({ "nInserted" : 1 })

我是node.js的新手,到目前为止一切看起来都不错,除了这个......完全坚持下去,不知道该尝试什么

编辑:

这是我的app.js代码,与路由有关吗?也许数据库附加到GET的req对象而不是POST?...

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/skdb');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// Make our db accessible to our router
app.use(function(req,res,next){
    req.db = db;
    next();
});

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

2 个答案:

答案 0 :(得分:1)

您需要从MongoError实例生成.message以诊断您的问题。

http://mongodb.github.io/node-mongodb-native/core/api/MongoError.html

版本问题:

Monk使用MongoDB和Mongoskin 1.4,请参阅package.json: https://github.com/Automattic/monk/blob/master/package.json

您可能需要使用支持更高版本驱动程序的其他模块(例如MongoDB节点包)。

在npm安装修士:

npm WARN peerDependencies The peer dependency mongodb@~1.4 included from mongoskin will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.

//other stuff

├── mpromise@0.5.1
├── debug@2.2.0 (ms@0.7.1)
├── mongodb@1.4.40 (kerberos@0.0.11, bson@0.2.22)
└── mongoskin@1.4.13

答案 1 :(得分:0)

正如Kinetics在评论中所说的 - 只需要查看res.json(err.message)而不仅仅是res.json(错误)

原来我有这个问题:MongoError: driver is incompatible with this server version