Mongoose: how to catch MongoError error caused by async callback

473 views
Skip to first unread message

Ádám Kecskés

unread,
Feb 23, 2016, 8:06:49 AM2/23/16
to Mongoose Node.JS ODM
When I'm trying to push too large data into `mongoDb` I'm getting this error: `MongoError: document is larger than the maximum size 16777216`

I know, it is normal, but I can not catch this error, and my `node.js` process exits.

Can you tell me, how to catch this error?

example 1: can not catch the error, `node` process exits
   
var mongoose = require('mongoose');
    mongoose
.connect('mongodb://localhost/test');
   
    mongoose
.connection.on('error', function (err) {
        console
.log(err);
   
});
   
   
var Cat = mongoose.model('Cat', {name: Object});
   
   
    setTimeout
(function () {
       
var kitty = new Cat({
            name
: {
                value
: 'Zildjian',
                data
: (new Array(16 * 1024 * 1024)).join("x")
           
}
       
});
   
        kitty
.save(function (err) {
           
if (err) // ...
                console
.log('meow');
       
});
   
   
}, 1000);


Without the timeout I can catch the error:

example 2: error is caught, process doesn't exit
 
   var mongoose = require('mongoose');
    mongoose
.connect('mongodb://localhost/test');
   
    mongoose
.connection.on('error', function (err) {
        console
.log(err);
   
});
   
   
var Cat = mongoose.model('Cat', {name: Object});
   
   
   
var kitty = new Cat({
        name
: {
            value
: 'Zildjian',
            data
: (new Array(16 * 1024 * 1024)).join("x")
       
}
   
});
   
    kitty
.save(function (err) {
       
if (err) // ...
            console
.log('meow');
   
});


I've tried to wrap my code into a `try...catch` but that doesn't caught the error either.

Valeri Karpov

unread,
Mar 11, 2016, 6:59:10 PM3/11/16
to Mongoose Node.JS ODM
Thanks for reporting this issue on github, this was fixed in 4.4.7: https://github.com/Automattic/mongoose/blob/master/History.md#447--2016-03-11
Reply all
Reply to author
Forward
0 new messages