File written to by the program not saving occasionally
Baconman321 (1103)

I have a program in Go that allows me to make and edit articles.

Sometimes, though the program won't save an edit of the article. The articles are stored via a .json file.

I have noticed that this usually happens if I close out of the window after it says it has successfully been edited, OR I close out of the computer. It always saves if I open the repl and check if it has been saved (as far as I know).

@tussiez told me that it was simply replit not saving sometimes, but I start to doubt that. It shows the edited article, but if I come back the next day the new edit is gone! It must be deleting the contents of the edit, but why? Also, how would I fix this?

Help would be appreciated.

- A confused bacon

You are viewing a single comment. View All
Andy_4sberg (54)

Replit doesn't allow repls to edit files. It creates a new instance of the repl and anything changed will just revert to how it was programmed. You should try using the Replit database, which doesn't reset unless you tell it to.

Baconman321 (1103)

@Andy_4sberg No, it does because it saves sometimes.

It's the program itself that is writing to the file, which many people have done before.

xxpertHacker (931)

@Baconman321 ^ they're right, once the Repl dies and turns off, the Repl is reset to the last point that a user had interacted with the Repl, therefore any programmatic changes are consequently reset.

Baconman321 (1103)


Makes sense, but IDK why it would save in the first place, then :/

What makes it save, and how do I write to files then?

I've seen people use files for storage, so how do those work?

xxpertHacker (931)

@Baconman321 a) it saves since the program is still executing, so the behavior is expected.

b) sure, plenty of replers do! They just encounter the bugs that you have ;)

Baconman321 (1103)

@xxpertHacker So, how would I make sure it saves?

If it saves because the program is still executing, then why does it have this problem at all? Wouldn't it always save because it always writes to the file while the program is executing?

Is there a surefire workaround for this, then?

Andy_4sberg (54)

@Baconman321 try running the program in the same tab as it's code on replit. Does it work? If it does, I think it works because it's running as the owner, and gives itself permission write. Does it work on the site in a new tab? If it does, I'm very confused. If it doesn't, I think the new tab is a new instance of the program, and the instance of it is greater on replit, so it overwrites the data (the one running in replit). This is just a theory, so of course I don't know if it's correct.

xxpertHacker (931)

@Baconman321 Once the program ends execution, the Repl's image is reset. Changes are temporary.

Solution: use a DB.

Baconman321 (1103)

@xxpertHacker DB is a bit flawed and limited rn, that's the problem.

Plus, file storage have much a higher size limit, thus why I use it :/

Baconman321 (1103)

@Andy_4sberg Wdym "same tab".

It's a web server that serves a web page which sends a fetch request back to the program which in turn writes to the file.