Can't write to JSON

I have scoured the forums for help writing to JSON files, and tried implementing everything I have found, to no avail. Here’s my project:

Writing to JSON works great when running on localhost. The moment I try running the same code with Glitch, everything still works, but the code won’t write to the JSON. Refresh from the terminal does nothing. If I manually update the JSON in the editor, the code will read from the JSON and update the interface accordingly; but it doesn’t go the other way. Does anybody know what’s going on?

1 Like

the code looks fine. does the code for updating the object log everything as expected? usually running refresh after the app writes will make the changes show up in the editor

Sorry it’s taken me so long to get back to this. I’m a church musician and the busy season is upon us.

I just threw in a console.log() statement into logData(), but I can’t get it to log in either the Logs tab or the Terminal tab. Same with another console.log() I put at the top of updateReport(). The latter function clearly works, because the report is updating with the interface. But I can’t get the logging to happen.

I agree that the code looks fine, but for whatever reason it’s not feeding into the JSON, even using the refresh command. I don’t really care whether the JSON updates in the editor, but I need the JSON to be accurate so that anybody who loads the interface sees the correct information fed by the JSON.

oh I just noticed in the client js

  fetch("http://localhost:3000/saveData", {

you’ll have to change that to something like

  fetch("saveData", {

as the user’s browser needs to send that to the glitch project, not to their own computer


extra thing: due to the way this code works

app.use(express.static("public"));

it means “when a request comes for http(s)://…/somefile.json, send back public/somefile.json if it exists,” so this code in the client

  return fetch("public/data.json")

should be

  return fetch("data.json")

Arg, I can’t believe I let localhost slip through. Rookie mistake. Then again I am a rookie.

Thanks also for the tip re. express. I am barely able to parse server-side JS at this point.

The two of these seem to have taken care of things. I’ll be back if it fails for some reason. Thank you!

1 Like