0

Here is my code which will get data from the postman and write to a file but while writing it replacing the existing data how to fix that in this code.

app.post('/postUser', function (req, res) {
    req.on('data', function (data) {
        console.log(data.toString());

        fs.writeFile('test.json', data, function (err) {
            if (!err) {
                console.log("Finished writing")
            }
        });
    });
});

My output should be like this.

[
  {
    "name":"Alpha",
    "password":"123",
    "id": 1
  },
  {
    "name":"beta",
    "password":"123",
    "id": 2
  }
]

When I use appendFile I am getting output like this :

[
    {
        "name": "alpha",
        "password": "123",
        "id": 4
    }
]  {
        "name": "sdad",
        "password": "123",
        "id": 4
    }
Dark Ninja
  • 149
  • 2
  • 13
  • Potential duplicate: https://stackoverflow.com/questions/3459476/how-to-append-to-a-file-in-node#11267583 – wmash Oct 19 '18 at 10:58
  • I need output like i mentioned this like shows to append if i use append it printing the data like this [ { "name":"Alpha", "password":"123", "id": 1 } ] { "name":"beta", "password":"123", "id": 2 } – Dark Ninja Oct 19 '18 at 11:01
  • Please see my potential solution below then :) – wmash Oct 19 '18 at 11:03

3 Answers3

0
app.post("/postUser", function(req, res) {
    req.on("data", function(data) {
        var newData;

        fs.readFile("test.json", function(readErr, fileData) {
            if (readErr) {
                console.error(readErr);
            }
            newData = JSON.parse(fileData).push(data);

            fs.writeFile("test.json", newData, function(writeErr) { 
                if (writeErr) {
                    console.error(writeErr);
                }
                console.log("Finished writing!");
            });
        });
    });
});

The way I usually get around this problem is to read the existing data and then append (push) your new data to the existing and write the result

wmash
  • 4,032
  • 3
  • 31
  • 69
0

The writeFile method will just replace the data if any present in the file or will create a file if not exists and write data to it.

Instead of fs.writFile use the appendFile method to append any data to the file.

app.post('/postUser', function (req, res) {
    req.on('data', function (data) {
        console.log(data.toString());

        fs.appendFile('test.json', data, function (err) {
            if (!err) {
                console.log("Finished writing")
            }
        });
    });
});
charan tej
  • 1,054
  • 10
  • 29
0

First read the file and append it with your changes and write it again.

app.post('/postUser', function (req, res) {
    req.on('data', function (data) {
        fs.readFile('test.json', 'utf8', function(err, file){
            //handle error
            if(!file) {
                var file = '';
            }
            var temp = JSON.parse(file);
            temp.push(data);    
            fs.writeFile('test.json', temp, function (err) {
                //handle error
                console.log("FILE STORED")
            });
        })
    });
});
Atishay Jain
  • 1,425
  • 12
  • 22