Saturday, 18 March 2017

Access content of JSON object

I am attempting to access the content of a JSON object returned from an API call in order to set variables.

var request = require('request');
var apiOptions = { server : "http://localhost:3000" };


var renderAdminLanding = function(req, res, body){
  var str = JSON.stringify(body.content, null, 2);
  res.render('admin_landing', {
    title: str});
}
module.exports.landing = function(req, res){
  var reqOptions, path;
  path = '/api/student';
  reqOptions = {
    url : apiOptions.server + path,
    method : "GET",
    json : true
  }
  request(reqOptions, function(err, response, body){
    renderAdminLanding(req, res, body);
  });
};

In this case body.content returns:

[ { "_id": "58ca92faa0c1e14922000008", 
    "name": "Smarty", 
    "password": "McSmartface", 
    "__v": 0, 
    "Courses": [] } ]

So body.content.name, for example returns nothing.



via Tjoudrey

No comments:

Post a Comment