I am using ExpressJS..When trying to fetch value from the req.body, in the console.log(req.body[ndx])(it prints { " c o..... like this as single character)
for (var ndx in req.body){
console.log(req.body[ndx]);
}
My req.body has the below nested JSON Object: How to 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;
for (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 the res.json value not the html page
Below is my 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 am product seller {{x.Name}} in {{x.City}} @ {{x.Country}}</p>
</div>
{{newRec[0].Name}}
</div>
<div data-ng-controller="nmsCtrl">
<p>Data to display</p>
<!-- <div data-ng-repeat="jsn in jsnData">
<p>I am product seller {{jsn.count}} displayed out of {{jsn.totalCount}}</p>
</div> -->
</div>
<div data-ng-controller="Store-Controller as 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 to Cart</button>
<product-panel></product-panel>
</div>
</div>
</div>
</body>
</html>
console.log(typeof res.body);?