Hello and thank you for taking the time to read this. I am currently trying to implement express-validator in my project. It's working well as it blocks posts if, for example, the name input is empty. However, I'm struggling to display the error message on the front end despite trying various methods. If anyone could provide some guidance, I would greatly appreciate it. Apologies for the lengthy message. Here is a link to the express-validator documentation that might be helpful:
Here is a snippet of my code...
<form id="sign-in">
// Form fields go here
</section>
<footer>
</footer>
<script type="module" src="/js/sign-in.js"></script>
Next, I have a fetch request in my sign-in.js file:
document.getElementById('sign-in').addEventListener('submit', event => {
// Event handling logic
const name = document.getElementById('name').value
const username = document.getElementById('username').value
...
window.fetch(`${config.serverHost}/sign-in`, {
method: 'post',
body: JSON.stringify({
name,
username,
...
})
}).then((res, errors) => {
if (res.status === 200) {
window.parent.location.reload()
} else if (res.status === 422) {
// Need help displaying error message here
}
})
Lastly, on the server side:
app.post('/sign-in', (req, res, next) => {
// Express validator //
// Validation checks
const errors = req.validationErrors()
if (errors) {
// Need assistance with displaying error message
} else {
// Success message
}
})