1

I've set up a node server that passes requests to a utility class. So far the POST request is hit but the mapping to the body property values are undefined. Bodyparser is also used in the post method to assist in the Json parse.

I stepped into the request and see that the body is populated and see that the property names are correct as shown in the paste below:

body: { '{\n\t"Email":"brian@gmail.com",\n\t"Dashboard_Name":"my dash 4",\n\t''},

But the below mapping to the values assinged via req.body.propertyname return undefined:

var p_email = req.body.Email;
var p_dashboardName = req.body.Dashboard_Name;

Question:

How can you parse JSON properties from request body in express server?

JSON object posted:

This is the JSON that I post to the server using Postman:

{
    "Email":"brian@gmail.com",
    "Dashboard_Name":"my dash 4"

}

Gist of the express server and associated utility method SaveUserProfile:

Express server -

var express     = require('express');
var UserLDAP  = require('./utilities/UserLDAP'); //utility file containing the POST method
var bodyParser = require('body-parser');


const url        = require('url');
const app        = express();
var sql          = require('mssql');
const cors       = require('cors');
const path       = require('path');


sql.connect("********************************************************************")
.then((connection1) => {

    sql.globalConnection = connection1;

    app.use(cors());

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


    app.post('/OOO/SaveUserProfile', UserLDAP.SaveUserProfile) 


    app.listen(process.env.PORT || 4000 );
    logger.info(`listening to port ${process.env.PORT}`);
}).catch((err) => {
    res.status(500).send(err.message);
    logger.error(err.message);
});

UserLDAP.js -

var sql = require('mssql');
var bodyParser = require('body-parser');




//Save User Profile

        exports.SaveUserProfile = function(req, res) {


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

        var request = new sql.Request(sql.globalConnection);

        console.log(req);

        var p_email = req.body.Email;
        var p_dashboardName = req.body.Dashboard_Name;


    };
Community
  • 1
  • 1
Brian Var
  • 6,029
  • 25
  • 114
  • 212

2 Answers2

1

Turns out I had incorrect content-type set in Postman on the object being posted. Needed to be set as:

application/json; charset=UTF-8
Brian Var
  • 6,029
  • 25
  • 114
  • 212
-1

Currently you have no way of knowing if a parser like body-parser.json has produced an error which seems the obvious place to start given the content is there but the result isn't.

I had a look at body-parser and found an issue that spoke to the problem of detecting a json error which I would expect to be good to know.

The developer suggested the following as one method.

app.use(errorFork(bodyParser.json(),   
function (err, req, res, next) { 
    // do stuff with only body parser errors 
})) 

// this is an example; you can use any pattern you like. 
function errorFork(middleware, errorHandler) { 
    middleware(req, res, function (err) { 
        if (err) {
            return errorHandler(err, req, res, next) 
        }else{
            return next() 
        }
    }) 
}

It isn't a fix but it would give you more info. Something is going wrong with the parsing by what you have indicated the questin is what? The other thing I noticed about your pasted body content is that it isn't valid json (ignoring \n\t) you have a few rouge ' in there, worth checking. Try copying what is in body (raw) and put it through a json validator site like jsonlint.com just as a double check and see if body-parser is returning any errors.

cmp-202
  • 312
  • 1
  • 5