D
D
Dvorak2016-11-19 13:52:36
Node.js
Dvorak, 2016-11-19 13:52:36

How to automatically overwrite JSON?

There is some JSON file file.json. I connect it and change the value of one field:

const file = require('./file.json');
file.text = 'text';

If I stop the script and open file.json, the text field stays the same, of course. Can I somehow make it so that file.text = 'text';the file itself is automatically overwritten after?
Z.Y. I know that you can just overwrite the file after each change using the fs module, but maybe there is an easier way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2016-11-19
@allishappy

I don’t know ready-made solutions, but if nothing is found, you can look towards setters (es6), where, after each assignment, call saving the file. In this case, you will have to rewrite the reading process to assign a setter to each read property.
https://developer.mozilla.org/en/docs/Web/JavaScript...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question