Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以自定义mongoose中的错误?_Mongoose - Fatal编程技术网

是否可以自定义mongoose中的错误?

是否可以自定义mongoose中的错误?,mongoose,Mongoose,假设我有一个模式: const transactionSchema = new mongoose.Schema({ "ID": { type:String, required:true, maxlength:36, minlength:35, } ... } 如果我的客户试图保存ID长度小于34的文档,mongoose将产生此错误 { "errors": { "ID": { "message

假设我有一个模式:

const transactionSchema = new mongoose.Schema({
    "ID": {
        type:String,
        required:true,
        maxlength:36,
        minlength:35,
    }
...
}
如果我的客户试图保存ID长度小于34的文档,mongoose将产生此错误

{
  "errors": {
    "ID": {
      "message": "Path `ID` (`k2131381a2asaddaddsdsw12sa3`) is shorter than the minimum allowed length (35).",
      "name": "ValidatorError",
      "properties": {
        "minlength": 35,
        "type": "minlength",
        "message": "Path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length (35).",
        "path": "ID",
        "value": "k2131381a2asaddaddsdsw12sa3"
      },
      "kind": "minlength",
      "path": "ID",
      "value": "k2131381a2asaddaddsdsw12sa3",
      "$isValidatorError": true
    }
  },
  "_message": "transaction validation failed",
  "message": "transaction validation failed: ID: Path `ID` (`k2131381a2asaddaddsdsw12sa3`) is shorter than the minimum allowed length (35).",
  "name": "ValidationError"
}
现在,为了告诉我的客户错误的确切原因(即ID比预期的短),我必须做大量的努力,并编写各种if-else语句,如下所示:

if(error && error.ID && error.ID.kind === 'minlength'){
   res.status(400).json({error:45});//error 45 refers to ID length errors. Client knows about it
}

我做错了吗?当ID长度不合适时,有没有干净的方法告诉mongoose只需给我一个代码45的错误?

您可以在
模式
定义中使用
验证
对象

验证
中的
消息
对象中,可以指定您选择的任何消息

const transactionSchema = new mongoose.Schema({
    "ID": {
        type:String,
        required:true,
        validate : {
            validator : (data)=>{
                if(data.length >=35 && data.length <=36 ){
                    return true; //validation success
                }
                else{
                    return false; // validation failure
                }
            },
            message : "Length mismatch. Length must be >=35 and <=36"
        }
    }
...
}
您可以打印
validationResult
对象,以查看
validationResult
对象具有哪些功能


对我们来说,最有用的对象是
validationResult.errors
对象,如上面代码中所示您可以在
模式
定义中使用
validate
对象

验证
中的
消息
对象中,可以指定您选择的任何消息

const transactionSchema = new mongoose.Schema({
    "ID": {
        type:String,
        required:true,
        validate : {
            validator : (data)=>{
                if(data.length >=35 && data.length <=36 ){
                    return true; //validation success
                }
                else{
                    return false; // validation failure
                }
            },
            message : "Length mismatch. Length must be >=35 and <=36"
        }
    }
...
}
您可以打印
validationResult
对象,以查看
validationResult
对象具有哪些功能

对我们来说,最有用的对象是上面代码中显示的
validationResult.errors
对象