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;
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