I am setting up a demo signup form where users can create an account. The signup credentials provided should be stored in-memory or to a user file (user.json) and can be used to login and be redirected to the user profile page.
I have created a user.json file and put a sample user details in it. I can use fetch to console.log() the user details from the user.json file, but how do I add more users to the file using fetch?
The code I have tried:
let bodyData = {
id: 2,
name: "sarah",
email: "sarah@gmail.com"
};
let postData = {
method: "POST",
body: JSON.stringify(bodyData)
};
// POST data to json file
fetch("./jsonfile/user.json", postData)
.then(data => console.log(data))
.catch(error => console.log(error));
// GET data form json file
fetch("./jsonfile/user.json")
.then(res => res.json())
.then(data => {
data = JSON.stringify(data);
console.log(data);
})
.catch(error => {
console.log(error);
});
I should be able to see in the output of console.log the new user added;