I tried it with fs module and it worked.
Thanks!
Am Samstag, 31. März 2018 19:37:55 UTC+2 schrieb Mark Volkmann:
>
> You have to use the built-in fs module to write the new JSON back to the
> file. You want something like this:
>
> const fs = require('fs');
>
> fs.writeFile('./data.json', JSON.stringify(file), err => {
> if (err) {
> console.error(err);
> } else {
> console.log('It worked!');
> }
> });
>
> On Sat, Mar 31, 2018 at 10:48 AM, <[email protected] <javascript:>>
> wrote:
>
>> Hi,
>>
>> I want to read and write json files. I'm new with nodejs so I don't have
>> much experience.
>> This is the content of my file and the structure:
>>
>> {
>> "products": {
>> "1234": {
>> "states": {
>> "STATEA": 0,
>> "STATEB": 0,
>> "STATEC": 0
>> },
>> "emailContacts": [
>> "[email protected] <javascript:>",
>> "[email protected] <javascript:>"
>> ]
>> },
>> "1256734": {
>> "states": {
>> "STATEA": 0,
>> "STATEB": 0,
>> "STATEC": 0
>> },
>> "emailContacts": [
>> "[email protected] <javascript:>",
>> "[email protected] <javascript:>"
>> ]
>> }
>> }
>> }
>>
>> I use this file because actually I dont't use a database.
>> Now I have a nodejs app which read states from receiving messages, if one
>> state is not normal I want to change the value of the state in the json
>> file ad save the state there. For this I tried this code:
>>
>> var file = require('./data.json')
>> console.dir(file.products['1234'].states['STATEA']);
>> file.products['1234'].states['STATEA']=5;
>> console.dir(file.products['1234'].states['STATEA']);
>>
>> It seems to work, but after stopping the nodejs app and looking into the
>> file there is no change in the file.
>> I tried different things but I didn't find a solution. I think the
>> problem is while running the app I only work on a copy of the file and not
>> on the original file...
>> I want to store the states in the file for a longer time. Later I want to
>> use a database instead a json file.
>>
>> Thanks for help! :)
>>
>>
>> --
>> Job board: http://jobs.nodejs.org/
>> New group rules:
>> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
>> Old group rules:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/nodejs/7e03a57f-0384-424f-97a1-9ed911b8a929%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/nodejs/7e03a57f-0384-424f-97a1-9ed911b8a929%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/8d5568ca-0214-40b2-af1d-3af89e14f77e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.