Custom Error Messages with Mongoose

前端 未结 8 480
旧巷少年郎
旧巷少年郎 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:05

    you can add a custom async validator. for example:

     {
          validator: (value) => {
            return UserModel.findOne({ email: value })
              .then(user => Promise.resolve(user == null))
              .catch(error => Promise.reject(false));
          },
          message: "User with this email already exists"
     }
    
    

提交回复
热议问题