Mongoose / Mongodb return and error handling meaning

I'm a little confused about the return value of Mongoldb update and how should I handle the error with it.

I am using Node.js, Express.js and Mongoose.js as my Mongodb driver

As I go through many tutorials, the only error handling method I have seen is ...

Example: simple custom schema .. and I want to update the phone number

Users 

{ 
  email : abc@abc.com,
  telephoneNumber : 123456
}

      

An example of error handling written in Node.js that many of them taught me

 Users.update({email: abc@abc.com}, {'$set': {telephoneNumber : 654321}, function(err, result){
      if(err){
           //err
      }else if(!result){
           //update not success
      }else{
           //update success
      }
 });

      

but when i go through Mongodb documentation i found that update returns WriteConcern value which returns something like this

 {
      "ok" : 1,             // update with no err
      "nModified" :1,        // successfully update 1 user
      "n" : 1               // found 1 
 }

      

So my question is if I should handle my error like this, so I would like to know more about update errors ...

  Users.update({email: abc@abc.com}, {'$set': {telephoneNumber : 654321}, function(err, result){
      if(err || result.ok === 0){
           //err
      }else if(result.nModified === 0){
           //update fail
      }else if(result.n === 0){
           //could not be found 
      }else{
           //update success
      }
 });

      

Is this a bad approach to update processing in mongoose / mongodb?

Thank!!:)

+3


source to share


1 answer


This is how we handle mongoose / mongodb errors. These could be errors such as "this value already exists" or similar problems.

First, in the mongoose call error block, add:

if (err) {
    return res.status(400).send({
                    message: errorHandler.getErrorMessage(err,req,res)
                });
}

      

Which calls the getErrorMessage function, which is defined in our errorHandler file, which can call the unique error message function. We also log errors in our mongo database in a separate collection.



exports.getErrorMessage = function(err,req,res) {
    var message = '';
    if (err.code) {
        switch (err.code) {
            case 11000:
            case 11001:
                message = getUniqueErrorMessage(err);
                break;
            default:
                message = 'Something went wrong. We have logged this issue and will correct';
        }
    } else {
        for (var errName in err.errors) {
            if (err.errors[errName].message) message = err.errors[errName].message;
        }
    }
    //log the error to Mongo
    ErrorLog.create(err,req,res);
    return message;
};

var getUniqueErrorMessage = function(err) {
var output;

try {
    var fieldName = err.err.substring(err.err.lastIndexOf('.$') + 2, err.err.lastIndexOf('_1'));
    output = fieldName.charAt(0).toUpperCase() + fieldName.slice(1) + ' already exists';

} catch (ex) {
    output = 'Unique field already exists';
}
return output;

      

};

Hope this helps, let me know if I can clarify anything.

+3


source







All Articles