How to catch the error when inserting a MongoDB document which violates an unique index?

后端 未结 3 434
庸人自扰
庸人自扰 2020-12-16 14:19

I\'m building a MEAN app.

This is my Username schema, the username should be unique.

var mongoose = require(\'mongoose\');
var Schema = mongoose.Sche         


        
相关标签:
3条回答
  • 2020-12-16 14:24

    You will need to test the error returned from the save method to see if it was thrown for a duplicative username.

    app.post('/authenticate', function(req, res) {
      var user = new User({
        username: req.body.username
      });
    
      user.save(function(err) {
        if (err) {
          if (err.name === 'MongoError' && err.code === 11000) {
            // Duplicate username
            return res.status(422).send({ succes: false, message: 'User already exist!' });
          }
    
          // Some other error
          return res.status(422).send(err);
        }
    
        res.json({
          success: true
        });
    
      });
    })
    
    0 讨论(0)
  • 2020-12-16 14:26

    Try this:

    app.post('/authenticate', function(req, res) {
            var user = new User({
                username: req.body.username
            });
    
            user.save(function(err) {
                if (err) {
                    // you could avoid http status if you want. I put error 500 
                    return res.status(500).send({
                        success: false,
                        message: 'User already exist!'
                    });
                }
    
                res.json({
                    success: true
                });
    
            });
        })
    
    0 讨论(0)
  • 2020-12-16 14:28

    You can also try out this nice package mongoose-unique-validator which makes error handling much easier, since you will get a Mongoose validation error when you attempt to violate a unique constraint, rather than an E11000 error from MongoDB:

    var mongoose = require('mongoose');
    var uniqueValidator = require('mongoose-unique-validator');
    
    // Define your schema as normal.
    var userSchema = mongoose.Schema({
        username: { type: String, required: true, unique: true }
    });
    
    // You can pass through a custom error message as part of the optional options argument:
    userSchema.plugin(uniqueValidator, { message: '{PATH} already exists!' });
    
    0 讨论(0)
提交回复
热议问题