Custom Error Messages with Mongoose

前端 未结 8 578
旧巷少年郎
旧巷少年郎 2021-02-01 04:06

So according to the mongoose docs, you are supposed to be able to set a custom error message in the schema like so:

 var breakfastSchema = new Schema({
  eggs: {         


        
8条回答
  •  轮回少年
    2021-02-01 05:00

    Just apply a middleware.

    recordingSchema.post('save', function (error, _, next) {
        next( error.code === 11000 
          ?   new Error('This item field already exist')
          :   error)
    });
    

提交回复
热议问题