1

I am using express 3.5 js. What I am trying to do is, get the form elements store it in a variable data as JSON and send it back to the server and update the JSON in the server.

In the browser I get the json output as {"name-check":true,"name-text":"someName","dest-check":false,"destination-text":"someplace"}

app.post('/json/*.json', function(req, res)  {
var outputFilename = '/tmp/my.json';
var mydata = req.body;

fs.writeFile(outputFilename, mydata , function(err) {
    if (err) {
        console.log(err);
    } else {
        console.log("JSON saved to " + outputFilename);
    }
});

When I try to update the json with the data from the form, it doesn't work.

I tried this post How can I pretty-print JSON using node.js? which works as long as I have the data in the same file, but doesn't work when getting it from the form.

Community
  • 1
  • 1
user2950017
  • 35
  • 1
  • 11
  • When i do req.body.namecheck(instead of name-check) I can see the value. But I need to get the whole json which is stored in javascript variable as {"name-check":true,"name-text":"someName","dest-check":false,"destination-text":"someplace"} – user2950017 Nov 16 '14 at 22:46
  • I have been able to get this one working. – user2950017 Nov 17 '14 at 03:51

1 Answers1

0

I first took the req.body in a variable and then used JSON.stringify.

user2950017
  • 35
  • 1
  • 11