For a school project, I am trying to validate users without the use of a real database. However, I am facing an issue where every mismatch in information entered is resulting in multiple errors. What I am aiming for is to consolidate these errors into just one notification (in case either the username or password does not match the stored JSON data). Below is the JavaScript code I am using:
const form = document.querySelector('form');
form.addEventListener('submit', function(e){
e.preventDefault();
const emailInput = document.querySelector('#email').value;
const pwdInput = document.querySelector('#pwd').value;
const object = {
email: emailInput,
pwd: pwdInput
};
fetch('users.json')
.then(res => res.json())
.then(data => {
data.forEach(function(user) {
const userInfo = JSON.stringify(user);
const databaseInfo = JSON.stringify(object);
if(userInfo === databaseInfo) {
console.log('success');
} else {
console.log('err');
}
});
})
.catch(error => console.log('error'));
});
Below is the JSON structure of the fake database:
[
{"email": "James", "pwd": "1111"},
{"email": "Peter", "pwd": "2222"},
{"email": "Josh", "pwd": "3333"}
]