-1

I have this code

/* 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);
    }
  });

});

The GET works fine, but the POST doesn't - the insert returns an error, and the error json is just:

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

The exact same insert works fine if i type it into the mongo console like this:

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

I'm new to node.js, everything seems good so far except for this.. completely stuck on it, not sure what to try

Edit:

This is my app.js code, is it a problem to do with routes? Maybe the db is attached to the req object for GET but not 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;
usrgnxc
  • 794
  • 2
  • 9
  • 34

2 Answers2

1

You need to produce the .message from MongoError Instance in order to diagnose your problem.

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

On version issue:

Monk uses MongoDB and Mongoskin 1.4, see package.json here: https://github.com/Automattic/monk/blob/master/package.json

You will probably need to use a different module (such as the MongoDB node package) that supports later drivers.

On npm install of monk:

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
Olivercodes
  • 1,048
  • 5
  • 17
  • i added an edit above, i think i do have the relevant code there (like i say, it does work for the GET request) – usrgnxc Feb 28 '16 at 20:53
  • Have you possibly specified the column as unique in your Mongo setup and do not have a defined error message? Try running the command in Mongo console twice. – Olivercodes Feb 28 '16 at 21:02
  • Also the MongoError instance should have a ```.message``` property. – Olivercodes Feb 28 '16 at 21:04
  • i've tried running it a few times in the console and it always works! so confusing... – usrgnxc Feb 28 '16 at 21:04
  • 1
    aha!! thank you! i thought the error json would have everything in it. phew. now i know what the error is: "driver is incompatible with this server version" - just need to figure out why that only happens for the insert and not the find – usrgnxc Feb 28 '16 at 21:07
  • assuming it will be this issue: http://stackoverflow.com/questions/30362304/mongoerror-driver-is-incompatible-with-this-server-version – usrgnxc Feb 28 '16 at 21:09
  • Yup, https://github.com/Automattic/monk/blob/master/package.json Monk uses MongoDB and mongoskin 1.4 – Olivercodes Feb 28 '16 at 21:15
0

As Kinetics says in comment - just needed to look at res.json(err.message) instead of just res.json(err)

Turns out i have this issue: MongoError: driver is incompatible with this server version

Community
  • 1
  • 1
usrgnxc
  • 794
  • 2
  • 9
  • 34
  • Edited answer above to reflect on your version problem. Hope that helps. Unfortunately, I don't think you will be able to use Monk without a bit of modifications to Monk itself. – Olivercodes Feb 28 '16 at 21:19
  • lovely stuff, i'll accept that answer rather than this one then. i think the easiest solution for now is for me to just use mongodb 1.4. – usrgnxc Feb 28 '16 at 21:21
  • i'll look into mongodb node package for future though, thanks again – usrgnxc Feb 28 '16 at 21:22
  • No problem. And I strongly suggest going with a later version of Mongo! https://www.mongodb.com/support-policy – Olivercodes Feb 28 '16 at 21:24
  • If you really want an external module for your MongoDB interactions, I strongly suggest taking a look at Mongoose. http://mongoosejs.com/ It's well documented and very widely used. – Olivercodes Feb 28 '16 at 21:25
  • i'll try without an external module first (i can just do it all with this?: https://docs.mongodb.org/getting-started/node/client/) – usrgnxc Feb 28 '16 at 21:30
  • The mongodb node driver should be able to provide you everything you need. However, I will say Mongoose is very useful for modeling. – Olivercodes Feb 28 '16 at 21:49