A
A
Andrey Dyrkov2015-10-23 19:22:06
MongoDB
Andrey Dyrkov, 2015-10-23 19:22:06

How to handle errors in mongoose?

How to handle errors such as writing a new document whose field should be unique but repeated?
At the moment, it brings down the whole process (
I know that you can rewrite the texts of errors in your own way, like

// customize within each schema or globally like so
  var mongoose = require('mongoose');
  mongoose.Error.messages.String.enum  = "Your custom message for {PATH}.";

I hung up a handler like
mongoose.connection.on('error',function(err){
  log.error('Could not connect to mongo server!');
});

But it actually only catches errors on the connection to the database.
How to catch errors like "MongoError:E11000 duplicate key..." ?
You can tell the errors of mongoDB itself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mark Doe, 2015-10-24
@mourr

And you process in the callback that you pass to .save. The error arrives there as the first parameter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question