ExpressJS Fetching value from nested JSON Object -


i using expressjs..when trying fetch value req.body, in console.log(req.body[ndx])(it prints { " c o..... single character)

for (var ndx in req.body){     console.log(req.body[ndx]); } 

my req.body has below nested json object: how extract count? req.body["count"] outputs undefined

{"count":1,"totalcount":38,"node":[{"categories":[],"foreignsource":null,"foreignid":null,"label":"172.20.96.20","assetrecord":{"description":null,"operatingsystem":null,"category":"unspecified","password":null,"id":2655,"username":null,"vmwaremanagedentitytype":null,"vmwaremanagementserver":null,"numpowersupplies":null,"hdd6":null,"hdd5":null,"hdd4":null,"hdd3":null,"hdd2":null,"hdd1":null,"storagectrl":null,"thresholdcategory":null,"enable":null,"connection":null,"autoenable":null,"cpu":null,"ram":null,"snmpcommunity":null,"rackunitheight":null,"admin":null,"additionalhardware":null,"inputpower":null,"vmwaremanagedobjectid":null,"vmwarestate":null,"vmwaretopologyinfo":null,"circuitid":null,"assetnumber":null,"rack":null,"slot":null,"region":null,"division":null,"department":null,"building":null,"floor":null,"room":null,"vendorphone":null,"manufacturer":null,"vendor":null,"modelnumber":null,"supportphone":null,"maintcontract":null,"maintcontractnumber":null,"maintcontractexpiration":null,"displaycategory":null,"notifycategory":null,"pollercategory":null,"vendorfax":null,"vendorassetnumber":null,"lastmodifiedby":"","lastmodifieddate":1433277477504,"dateinstalled":null,"lease":null,"leaseexpires":null,"managedobjectinstance":null,"managedobjecttype":null,"serialnumber":null,"port":null,"comment":null},"lastcapsdpoll":1433277477793,"createtime":1433277477504,"labelsource":"a","type":"a","id":"10"}]} 

my code:

var bodyparser = require('body-parser');  var urlencodedparser = bodyparser.urlencoded({ extended: true }); app.use('/index', function(req, res, next){         var getreq = http.request(options, function(res) {         res.setencoding('utf8');         res.on('data', function (chunk) {             jsnarry += chunk;           });              res.on('end', function (chunk) {                          req.body = jsnarry;             (var ndx in req.body){                 console.log(req.body[ndx]);             }             next();         });         }).end(); }); app.use(bodyparser.json({ type: 'application/*+json' }));     app.use('/index', urlencodedparser, function(req, res, next){     res.send(req.body);      next(); }); 

console.log(json.parse(req.body)) o/p below ones

{ count: 1,   totalcount: 38,   node:    [ { categories: [],        foreignsource: null,        foreignid: null,        label: '172.20.96.20',        assetrecord: [object],        lastcapsdpoll: 1433277477793,        createtime: 1433277477504,        labelsource: 'a',        type: 'a',        id: '10' } ] }  var options = {           host: host,           method: 'get',           headers: {               'accept' : 'application/json'           }     };  res.json(info["totalcount"]); res.sendfile(path.join(_dirname, '/html', 'index.html')); 

//only shows res.json value not html page

below html file:

<!doctype html> <html data-ng-app="shopstore"> <head> <meta charset="iso-8859-1"> <title>simple angular testing</title> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"></link> <link rel="stylesheet" type="text/css" href="css/main.css"></link> </head> <body>     <script type="text/javascript" src="js/lib/angular.min.js"></script>     <script type="text/javascript" src="js/lib/d3.min.js"></script>     <script type="text/javascript" src="js/product.js"></script>     <script type="text/javascript" src="js/app.js"></script>      <div data-ng-controller="scopectrl">         <div data-ng-repeat="x in newrec">              <p>i product seller {{x.name}} in {{x.city}} @ {{x.country}}</p>         </div>         {{newrec[0].name}}       </div>      <div data-ng-controller="nmsctrl">      <p>data display</p>     <!--    <div data-ng-repeat="jsn in jsndata">              <p>i product seller {{jsn.count}} displayed out of {{jsn.totalcount}}</p>         </div> -->     </div>        <div data-ng-controller="store-controller store">       <div data-ng-repeat="product in store.products">         <div data-ng-hide='product.cantpur'>             <h6>product:::{{product.item}}</h6>             <h6>dollar price:::{{product.dollar | currency}}</h6>             <h6>description::::{{product.desc}}</h6>             <h6>{{review.stars}}</h6>             <button data-ng-show='product.canadd'>add cart</button>             <product-panel></product-panel>         </div>        </div>     </div> </body> </html> 

do include body-parsing middleware? body-parser support json parsing.

var app = require('express')(); var bodyparser = require('body-parser'); app.use(bodyparser.json()); 

bodyparser.json expect receive payload in application/json content type. example using jquery ajax:

.ajax({   url:url,   type:"post",   data:data,   contenttype:"application/json; charset=utf-8",   datatype:"json",   success: function(){     ...   } }) 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -