1

Here I call the module. Then ask for the response. then I use JSON.parse the body of the response but the output looks like this.

{ response:
   { status: 'ok',
     userTier: 'developer',
     total: 240,
     startIndex: 1,
     pageSize: 1,
     currentPage: 1,
     pages: 240,
     orderBy: 'newest',
     results: [ [Object] ] } }

I want to be able to see the Objects

    {
    "response": {
        "status": "ok",
        "userTier": "developer",
        "total": 240,
        "startIndex": 1,
        "pageSize": 1,
        "currentPage": 1,
        "pages": 240,
        "orderBy": "newest",
        "results": [{
            "id": "sustainable-business/2017/jun/13/battery-storage-and-rooftop-solar-could-mean-new-life-post-grid-for-consumers",
            "type": "article",
            "sectionId": "sustainable-business",
            "sectionName": "Guardian Sustainable Business",
            "webPublicationDate": "2017-06-12T23:51:00Z",
            "webTitle": "Battery storage and rooftop solar could mean new life post-grid for consumers",
            "webUrl": "https://www.theguardian.com/sustainable-business/2017/jun/13/battery-storage-and-rooftop-solar-could-mean-new-life-post-grid-for-consumers",
            "apiUrl": "https://content.guardianapis.com/sustainable-business/2017/jun/13/battery-storage-and-rooftop-solar-could-mean-new-life-post-grid-for-consumers",
            "isHosted": false
        }]
    }
}

this is the code

function prettyJSON(data) {
 return JSON.stringify(data,null,"   ");
}


api.custom.search({fromDate:"2017-06-12", 
     toDate:"2017-06-12", 
     orderBy:"newest",
     //showFields:"all",
     pageSize: 2})
 .then(function(response){
  var reqBody = response.body.toString(); 
  reqBody = JSON.parse(reqBody); 
  console.log(reqBody); 
   
 })
 
  .catch(function(err){
  console.log(err); 
 }); 
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
SunCity
  • 11
  • 1

1 Answers1

1

I believe a console log will not output nested objects so your response is probably fine you just cannot see it in your console.

If you want to inspect it you can either use a debugger and inspect your variable there or use util.inspect()

See this answer

Edit: The console in Chrome will show the nested objects but node's console does not extend objects by default - thanks to Jeremy Thille for his comment

cdimitroulas
  • 2,380
  • 1
  • 15
  • 22