gpt4 book ai didi

node.js - Mongoose Schema 数字字段的长度是准确的

转载 作者:可可西里 更新时间:2023-11-01 09:33:51 25 4
gpt4 key购买 nike

我有这个 moongoose 模式:

var userSchema = new mongoose.Schema({
firstname: {
type: String,
required: true,
min: 3,
max: 24
},

lastname: {
type: String,
required: true,
min: 3,
max: 24
},

id: {
type: Number,
required: true,
min: 9,
max: 9
},

mediations: [assetSchema]
});

当我尝试添加 ID 为 320981350 的新用户时,出现下一个验证错误:

{
"errors": {
"id": {
"message": "Path `id` (320981350) is more than maximum allowed value (9).",
"name": "ValidatorError",
"properties": {
"max": 9,
"type": "max",
"message": "Path `{PATH}` ({VALUE}) is more than maximum allowed value (9).",
"path": "id",
"value": 320981350
},
"kind": "max",
"path": "id",
"value": 320981350,
"$isValidatorError": true
}
},
"_message": "User validation failed",
"message": "User validation failed: id: Path `id` (320981350) is more than maximum allowed value (9).",
"name": "ValidationError"
}

是否有任何其他方法可以验证 Number 类型字段的准确长度?还是我误解了 Mongoose 存储数字的方式?

最佳答案

minmax 表示提供的Number 中允许的位数,如错误所述:

(320981350) is more than maximum allowed value (9)

他们的意思是 Number 类型的字段的实际最小值/最大值,例如

{
type: Number,
min : 101,
max : 999
}
  • 允许的最大 Number999
  • 允许的最小 Number101

在您的情况下,如果您有 9 位数字作为您的 id,请在架构中定义字段,如:

{
type: Number,
min : 100000000,
max : 999999999
}

关于node.js - Mongoose Schema 数字字段的长度是准确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com