嘿,我想为我的上传网页建立一个消息系统。
我使用express-validator和connect-flash,表达消息
app.js
const { check, validationResult } = require('express-validator');
.
.
.
app.post('/metin/ekle', [
// username must be an email
check('baslik', 'Baslik Gereklidir').notEmpty(),
// password must be at least 5 chars long
check('konu','Konu gereklidir').notEmpty(),
],function(req, res){
const errors = validationResult(req);
if (!errors.isEmpty()) {
req.flash('danger', { errors: errors.array() });//I dont know what do do exactly here
}
var metin = new Metin({
baslik: req.body.baslik,
yazar: req.body.yazar,
konu: req.body.konu,
});
metin.save(function(err){
if(err){
console.log(err);
return;
} else {
req.flash('success', 'Metin Eklendi');
res.redirect("/metin/"+ metin._id);
}
});
});
这是我的article.pug:
!= messages('my_message_template', locals)
if errors
each error, i in errors
div(class="alert alert-danger") #{error.msg}
ReferenceError:未定义味精
我不知道在取消validationResult错误时该怎么做。我写了味精,但我知道这是错误的。遇到快递验证错误时,我该如何更改密码以发出危险?
答案 0 :(得分:0)
您需要在行下方进行更改
req.flash({msg});//I dont know what do do exactly here
收件人
return res.status(422).json({ errors: errors.array() });
然后在视图中可以找到类似的内容:
{
"errors": [{
"location": "body",
"msg": "Invalid value",
"param": "username"
}]
}
您可以看到express-validator
文档here
谢谢