【发布时间】:2017-11-30 02:37:52
【问题描述】:
我没有收到错误,即使当我没有输入有效的 url 时架构应该会失败。
我可以确认来自前端的数据正在正确发送。它正在通过路线传递给 Mongoose(如下所示)。
Hosts.Create(req.body, func...) 函数的回调没有 Err。
这是架构。验证器:[validators.isURL()] 没有产生消息。
var HostSchema = new Schema({
domain: {
type: String,
required: [ true, 'A Domain is required' ],
// This is the broken validator
validator: [ validators.isURL({message: 'Must be a Valid URL', protocols: ['http','https','ftp'], require_tld: true, require_protocol: true} ) ]
},
pkg: {
type: String,
required: [ true, 'Hosting Package is required' ]
},
ssl: { type: Boolean, required: true },
maint: { type: Boolean, required: true },
...
});
我的路线文件:
// Process Add Cx
hosting.post('/add', function(req, res, next) {
// If No Request data.
if (req.body.constructor === Object && Object.keys(req.body).length === 0) {
... Send View if no form data ...
} else { // Proccess Data
// Create new User
Hosts.create(req.body, function(err, host) {
if (err) return res.json({success: false, message: Hosts.MongoErrors(err)});
// If everything was sucessful! Yay!
res.json({success: true, message: 'Host Successfully Saved!'});
});
}
【问题讨论】:
标签: node.js mongodb validation mongoose