简体   繁体   中英

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

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.

OP has clarified that they are using Express in their web application, but isn't using anything to process the incoming JSON. I recommend using the body parser library to process the incoming JSON. Here is an example...

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.json());

Edit: It looks like you're using Express v4, so you can bypass using bodyparser and use express to process JSON...

app.use(express.json());

You should read the req value, not the res value. Like this...

let data = req.body;

Then, write the JSON data to your file...

collectReqData(req, res => {
  fs.writeFileSync('signUp.json', req.body);
});

Also, I recommend adding error handling & returning a response from your method. Like this...

collectReqData(req, res => {
  try {
    fs.writeFileSync('signUp.json', req.body);
    res.status(200);
  } catch (err) {
    res.status(500).json(err)
  }
});

Can't recreate your server code behavior. Not enough information. But @jfarleyx answer looks legit to me. Except that you probably do not use express framework, judging by the look of collectReqData() . Never the less, the root issue is with the client code: you should change request.send(formElement); to request.send(JSON.stringify(formElement));

Updated to reflect on comments After looking into repl.it I see that your problem is not with the code in the question, but exactly with the skipped code. Specifically with collectReqData() and collect() I do not know how you want them to be/ so I fixed the code my way, using body-parser and not trying to be fancy. You can check correctly working code at https://repl.it/repls/SadCulturedFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM