pittuzzo
pittuzzo

Reputation: 491

NodeJs - nested JSON POST input

I need to read nested JSON data in my Node.js server sent with a POST form.

For testing I'm using Postman with a POST request at 127.0.0.1:8080/extractByCenter, with Header Content-Type = application/json and Body as "raw" JSON (application/json) with the following data:

{
    "center": {
        "lng":17.4152,
        "lat":40.4479
        },
    "radius":20
}

My main .js file launched with node is very simple:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');

//------------------------------------
//Configuration
//------------------------------------
var config = require('./config.js');
app.use(morgan('dev')); //remove dev at the end of development
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//------------------------------------
//Routes
//------------------------------------
app.use('/',    require('./routes/maprouter.js'));

//------------------------------------
//Server Start
//------------------------------------
mongoose.connect(config.DATABASE);
app.listen(config.SERVICE_PORT);
console.log('server start at port ' + config.SERVICE_PORT);

Where the maprouter.js just use a function of the MapManager.js:

var express = require('express');
var router = express.Router();
var MapManager = require("../managers/MapManager");

router.route('/extractByCenter').
    post(function(req,res){
        MapManager.extractByCenter(req.center,req.radius,function(err,data){
            res.json({
                success: true,
                message: 200,
                data: data
            });
        });
    });

This function just takes the JSON data center and radius and it performs the query on the DB.

var _ = require('lodash');
var config = require('../config.js');
var GeoJSON = require('../models/GeoJSON.js');

var Manager = {
    extractByCenter: function(center,radius,callback){
        var query = {
            'coordinates' : {
                $near: {
                $geometry: {
                    type: "Point" ,
                    coordinates: [ center.lng , center.lat ]
                },
                $maxDistance: radius,
                $minDistance: 0
                }
            }
        };
        GeoJSON.find(query,'-__v -_id',callback);
    }
}

module["exports"] = Manager;

The problem is the reading of the center data [ center.lng , center.lat ]: I have a "TypeError: Cannot read property 'lng' of undefined"

Why center.lng (and surely also center.lat) doesn't work?

Upvotes: 1

Views: 1785

Answers (3)

PANKAJ NAROLA
PANKAJ NAROLA

Reputation: 164

try

req.body

make sure that body parser has to be there

Upvotes: 0

Paul
Paul

Reputation: 36319

The bodyParser places things on req.body, not on req. So change your code to be req.body.center and req.body.radius and you should be good to go.

Upvotes: 3

JacobW
JacobW

Reputation: 876

When you post to express, the body parser puts the request body into the body object. You're trying to access it off of req.center and req.radius instead of req.body.center, req.body.radius.

req.body.center, req.body.radius

var express = require('express');
var router = express.Router();
var MapManager = require("../managers/MapManager");

router.route('/extractByCenter').
    post(function(req,res){
        MapManager.extractByCenter(req.body.center,req.body.radius,function(err,data){
        res.json({
            success: true,
            message: 200,
            data: data
        });
    });
});

Ninja Edit:

Try doing a console.log of the req object so you can see all of the different things that it contains.

Upvotes: 1

Related Questions