我有一个像这样的猫鼬计划......
lightbox_opacity:{type:Number, min:0, max:1}
我有 2 个问题...
当我尝试插入字符串“abc”时,它会悄悄地忽略此字段插入。架构中的其余字段已成功插入。我的印象是它会抛出异常。可以这样做吗?
如果我尝试插入 5,它只是允许它,并且看起来 min 和 max 根本没有发挥作用。
我缺少什么?
验证可以帮助您。一个例子如下。
var min = [0, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
var max = [1, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
var numberschema = new mongoose.Schema({
n: {type: Number, min: min, max: max}
});
var numberschema = mongoose.model('number', numberschema, 'number');
var insertDocuments = function(db) {
var a = new numberschema({
n: 5
});
console.log(a);
a.validate(function(err) {
if (err)
console.log(err);
});
a.save(function (err, ack) {
if (err) {
console.log('Mongoose save error : ' + err);
} else {
console.log('Mongoose save successfully...');
}
});
};
Run Code Online (Sandbox Code Playgroud)
当尝试插入时5
,出现如下错误
{ [ValidationError: Validation failed]
message: 'Validation failed',
name: 'ValidationError',
errors:
{ n:
{ [ValidatorError: The value of path `n` (5) exceeds the limit (1).]
message: 'The value of path `n` (5) exceeds the limit (1).',
name: 'ValidatorError',
path: 'n',
type: 'max',
value: 5 } } }
Mongoose save error : ValidationError: The value of path `n` (5) exceeds the lim
it (1).
Run Code Online (Sandbox Code Playgroud)
当尝试插入时abc
,出现如下错误
Mongoose save error : CastError: Cast to number failed for value "abc" at path "
n"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14564 次 |
最近记录: |