0

Hello I'm simply trying to have a welcome message that gets updated via POST, and as far as I can tell I'm sending a JSON from my Client side JavaScript, and on my NodeJS side it shows as [object object] I've tried req.body and that comes back with "undefined". I'm wondering how I would be able to extract my welcome message from the JSON i'm sending to the nodejs server and save to a .JSON to be able to be pulled later from the client.

I've tried doing jsonstringify(req) and that returns a big error in my nodejs cmd which I can paste if that may be necessary.

nodejs server POST, and it will write to the file welcome.json, it will either write [object object] or undefined, based on if I use req.body or req.

app.post('/update', function (req, res) {
    fs.writeFile(__dirname + '/rqsts/welcome.json', req.body, function () {
        console.log('We got a Post request' + req.body);
    });
});

and here is my client side http post request:

function submit() {
    var text_Input = document.getElementById('textinput').value;
    var testing = document.getElementById('testme');
    var welcome_array = {
        welcome: ""
    };
    welcome_array.welcome = text_Input;
    var welcomeJSON = JSON.stringify(welcome_array);
    var url = 'http://localhost:8080/update';
    var http = new XMLHttpRequest();
    http.open('POST', url, false); // false for synchronous request
    Add_Para(welcomeJSON, testing);
    http.send(welcomeJSON);
}

the Add_Para is a function I made to troubleshoot, it adds a paragraph to said html with the requested data "welcomeJSON"

1
  • I receive this when I try JSON.stringify(req) TypeError: Converting circular structure to JSON Commented Apr 30, 2019 at 2:50

4 Answers 4

1

If you are using expres 4.16 or higher use

app.use(express.json());

There is no need to use bodyParser

Sign up to request clarification or add additional context in comments.

3 Comments

I am on 4.14 as far as my dependencies say. Can I just update my package.json to 4.16 and it will work with that?
You're correct, I updated my version and made my source code to fit this. thank you.
You're welcome Please accept the answer if you find it working.@DevinB
0

Try

console.log('We got a Post request %O', req.body);

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Console

Example:

const a = {
  a: 'a',
};
console.log('We got a Post request %O', a); // We got a Post request { a: 'a' }

Or you may try using JSON.stringify

Example:

const a = {
  a: 'a',
};
console.log('We got a Post request ' + JSON.stringify(a)); // We got a Post request {"a":"a"}

6 Comments

We got a Post request undefined
It's logging the variable correctly, I just cant seem to transfer this object into a JSON, even though it's been turned into a JSON before being sent.
You may try printing out req first to see what is inside. At least it will not log [object object] to help you to troubleshoot the problem.
Are you meaning to make the json array on Nodejs side and attach that to the request that comes in?
When I try req it’s [object object] req.body returns undefined
|
0

Hello I have found my solution, I believe I was missing including body-parser was the main issue, here is my now update code.

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.post('/update', function (req, res) {
    console.log('We got a Post request "%O"', req.body.welcome);
    var now = new Date();
    console.log('Time of change: ' + now.getHours() + ":" + now.getMinutes() + " " + now.getDate() + "/" + now.getMonth() + "/" + now.getFullYear());
    fs.writeFile(__dirname + '/rqsts/welcome.json', JSON.stringify(req.body), function () {
    });
});

client side js

function submit() {
    var text_Input = document.getElementById('textinput').value;
    var testing = document.getElementById('testme');
    var welcome_array = {
        "welcome": ""
    };
    welcome_array.welcome = text_Input;
    var welcomeJSON = JSON.stringify(welcome_array);
    var url = 'http://localhost:8080/update';
    var http = new XMLHttpRequest();
    http.open('POST', url, false);
    http.setRequestHeader('Content-type', 'application/json')
    Add_Para(welcomeJSON, testing);
    http.send(welcomeJSON);
}

Comments

0

Are you using 'body-parser' into express to read req.body?

Body-parser will help you in extracting req.body content, follow below link for how to use in Node JS. https://blog.fullstacktraining.com/how-do-you-extract-post-data-in-node-js/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.