如何从go: github.com/coreos/go-oidc@v2@v2.0.0+incompatible: invalid github.com/ import path "github.com/coreos/go-oidc@v2"
go: error loading module requirements
中检索"msg"
对象?
整个数组的输出如下:errors.array()
我尝试用[{"value":"abc","msg":"Your email is not valid","param":"email","location":"body"}]
指定"msg"
对象,但没有返回任何内容。
errors.array()[1]
我的静态文件中输出错误:
router.post("/signup",
[
check('email', 'Your email is not valid').not().isEmpty().isEmail().normalizeEmail(),
check('password', 'Your password must be at least 5 characters').not().isEmpty().isLength({min: 5})
],
function (req, res, next) {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.render("user/signup", {
hasErrors: JSON.stringify(errors.array()[1])
});
} else {
//...
}
});
答案 0 :(得分:0)
如果要访问错误,可以使用.array()
或.mapped()
。否则,您可以使用doc
中显示的.formatWith
:
app.post('/create-user', yourValidationChains, (req, res) => {
// errors will be like [{ myLocation: 'body' }, { myLocation: 'query' }], etc
const errors = myValidationResult(req).array();
});
或者, formatWith :
app.post('/create-user', yourValidationChains, (req, res, next) => {
const errorFormatter = ({ location, msg, param, value, nestedErrors }) => {
// Build your resulting errors however you want! String, object, whatever - it works!
return `${location}[${param}]: ${msg}`;
};
const result = validationResult(req).formatWith(errorFormatter);
if (!result.isEmpty()) {
// Response will contain something like
// { errors: [ "body[password]: must be at least 10 chars long" ] }
return res.json({ errors: result.array() });
}
// Handle your request as if no errors happened
});