Basically, what I want to do is:
I have this data that I get using XHR and display it in browser.
[
{
"name": "task1",
"todo": "Complete online Javascript course"
},
{
"name": "task2",
"todo": "Jog around the park 3x"
},
{
"name": "task3",
"todo": "10 minutes meditation"
}
]
let dataUse = []
getData = () => {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status ==200) {
const myData = JSON.parse(this.responseText)
dataUse.push.apply(dataUse, myData)
}
}
xhttp.open("GET", "data.json", true);
xhttp.send();
}
getData()
And what I want to do is, I want to have a box where I can add new data to the JSON file.
saveData = () =>{
myData.push(newData)
const fs = require('fs')
const finished = (error) =>{
if(error){
console.error(error)
return
}
}
const jsonData = JSON.stringify(myData, null, 2)
fs.writeFile('data.json', jsonData, finished)
}
The problem is this line of code needs to be run through the terminal in order for it to write the new data into the file, so I want to know if there is a way to run the terminal in browser through some kind of function or something.