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.
fs.writeFileSync('signUp.json', JSON.stringify(data));"fName": "example"- close its key/value with double quotes and execute againJSON.stringify(res).app.post('/', function (req, res) { // get the request parameters let params = req.params; // write the params to a file fs.writeFileSync('signUp.json', JSON.stringify(params)); // write your response using res.send res.send({sucess: true}); })collectReqData? Also, you should not be synchronously writing to a file in this context as you will be blocking the thread for other requests.