2

I have a problem with getting data from client side in NodeJS.

On the client side I've prepared JSON data as you can see in codepen

On the server side I'm trying to get these data from the client:

var express = require('express');
var mysql = require('mysql');

var app = express();

app.use('/', express.static('../client/app'));
app.use('/bower_components', express.static('../client/bower_components/'));

var server = require('http').createServer(app);

var bodyParser = require('body-parser');
app.jsonParser = bodyParser.json();
app.urlencodedParser = bodyParser.urlencoded({ extended: true });

//mysql connection setup
var connection = mysql.createConnection({
    host : "localhost",
    port: "3306",
    user : "root",
    password : "",
    database : "db",
    multipleStatements: true
});

app.get("/cities", function(req, res) {

    console.log(res.body); //I'm getting nothing

    var data= {
        city: req.body.city,
        country: req.body.country
    };

    var query = connection.query('INSERT INTO table SET ?', data, function(err, res) {
        if (err) {
            console.log(err);
        } else {
            console.log('success');    
        }
    });
    console.log(res); 
});
    
server.listen(3000, function () {
    'use strict';
});

What I'm doing wrong? Is there a way to debug NodeJS in ST? Thanks.

2
  • database : "", no db selected. Commented Jan 21, 2016 at 9:28
  • Sorry, I forgot to include it in example above, but that isn't a problem. Thanks Commented Jan 21, 2016 at 9:33

2 Answers 2

1

Please try this req.params.city to fetch input data. It works in my project. However, req.body I use for getting data sent through POST method.

app.get - use req.params app.post - use req.body

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

4 Comments

I've changed to req.params, but I still cannot get data from client :/ { city: undefined, country: undefined }
try : app.get("/cities/:city/:country", function(){ console.log(req.params.city); }); AND send in url like : localhost:3000/cities/london/britain
I would like to store all data from JSON (not specific field like ) to db and I'm a little bit confused about API form in NodeJS. Do you have any suggestion for that solution? Thanks.
I would suggest using :- var bodyParser = require('body-parser'); app.use(bodyParser.json()); app.post("/cities", function(){ console.log(req.body); }); this will work when request sent in post (x-www-form-urlencode)
1

Instead of doing:

console.log(res.body); //I'm getting nothing

I think you have to send the response as res.send('success'). Or you might try sending the posted values again to test:

res.send(data);

like:

var query = connection.query('INSERT INTO table SET ?', data, function(err, res) {
    if (err) {
        console.log(err);
    } else {
        console.log('success');  
        res.send(res.insertId);  
    }
});

1 Comment

When I'm trying to access api (cities), I got TypeError: Cannot read property 'city' of undefined error :/ It's a first property of data object.

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.