[英]How to display express validator errors in a div
I'm using express validator to validate a form and I'm able to get the results however, I'm not sure how to display it into my EJS file.我正在使用快速验证器来验证表单并且我能够获得结果,但是我不确定如何将它显示到我的 EJS 文件中。
const errors = validationResult(req);
if (!errors.isEmpty()) {
let error = errors.array().map(i => `${i.msg}`).join(' ');
res.render('sell', { title: 'Sell', error: errors });
}
ejs file ejs文件
<% if (error != null) { %>
<div class="alert alert-danger">
<%= error %>
</div>
<% } %>
This code only returns the errors on one line, I want it to display the errors in its own alert div.此代码仅在一行中返回错误,我希望它在自己的 alert div 中显示错误。
create a for loop for errors like this为这样的错误创建一个 for 循环
<% if (error !=null) { %>
<% for (const err of error) { %>
<div class="alert alert-danger">
<%= err %>
</div>
<% } %>
<% } %>
and I would actually rename it to errors since it's plural我实际上会把它重命名为错误,因为它是复数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.