猫鼬 - 验证验证是什么?

时间:2014-08-26 00:59:45

标签: mongoose

在关注this教程之后,我来到第5步(我适应了自己的应用程序)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var Unit = new Schema({
    name: {
        type: String,
        require: true
    },
    screenName: {
        type: String,
        require: true
    },
    position: {
        type: [Number],
        require: true
    },
    imagePath: {
        type: String,
        require: true
    },
    state: {
        type: String,
        require: true
    }
});

Unit.path('model').validate(function(v) {
    return ((v!="") && (v!= null));
});

module.exports = mongoose.model('Unit', Unit);

...当我尝试启动服务器时,它会给我TypeError: Cannot call method 'validate' of undefined

删除验证块可以使一切正常运行。

但块实际上在做什么?验证单元模型的“路径”?什么路? the mongoose docs中的任何内容都没有给我任何指导。

1 个答案:

答案 0 :(得分:1)

该代码正在向model架构的Unit字段(又名path)添加自定义验证功能。但该架构没有model字段,因此对Unit.path('model')的调用会返回undefined,导致您看到的TypeError

验证函数是一种增强的require: true验证器示例,它也不允许空字符串和空值。

相关问题