Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139982

Trying to store input data on json file with Node.js

$
0
0

As I said on title I'm trying to store the inputs the user made on an HTML form using fs from node to store it on a JSON file.

What I'm trying to save is a key value pair which would look like this:

{
  fName: example,
  lName: example,
  email: example,
  pswd: example
};

But what I'm getting is:

{ '[object Object]': '' }

I got two files working with this, one which verify the inputs and makes an AJAX call if everything is fine and another one which handles the HTTP request and saves the data on the JSON file.

Here, the AJAX call:

let formElement = {
        fName: firstName,
        lName: lastName,
        email: email,
        pswd: pswd
      };
var request = new XMLHttpRequest();
request.open('POST', './signUp.js');
request.setRequestHeader("Content-type", "application/json");
request.send(formElement);

And here the code which manages the HTTP request:

collectReqData(req, res => {
  let data = JSON.parse(JSON.stringify(res));
  fs.writeFileSync('signUp.json', data);
});

I've also tried doing let data = JSON.stringify(res); but I'm getting the same response.

If someone could help me I'll be really glad, I'm stuck with this and I don't know what to do.


Viewing all articles
Browse latest Browse all 139982

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>