I am trying to create a login form and the functionality I want is the following: every time the user enters his email and password, it should get stored as a new JSON object in a JSON file. Right now, what’s happening is that a single object is getting created and it is being overwritten every time the user logs in. I want a new object for every login attempt. Here is the code:
app.post("/login", validate, (req, res) => {
let userDataArray = new Array();
let obj = new Object();
let email = req.body.email;
let password = req.body.password;
obj.email = email;
obj.password = password;
// const jsonString = JSON.stringify(user);
userDataArray.push(obj);
let jsonString = JSON.parse(JSON.stringify(userDataArray));
fs.writeFile("data.json", JSON.stringify(jsonString), () => {
console.log("Written to file");
});
res.send(`Email: ${email} Password: ${password}`);
});