Wednesday, 31 May 2017

Transforming results for consumption by client in nodejs

With res.json(results); I get appropriate json response like below.

{
  "Flights": {
  "SessionKey": "3510bebbd7604df490d44ad50431b741_rrsqbjcb_06a13f0a788e803fcc56e78802891a26",
"Query": {
  "Country": "GB",
  "Currency": "GBP",
  "Locale": "en-gb",
  "Adults": 1,
  "Children": 0,
  "Infants": 0,
  "OriginPlace": "11235",
  "DestinationPlace": "4698",
  "OutboundDate": "2017-06-05",
  "InboundDate": "2017-06-06",
  "LocationSchema": "Default",
  "CabinClass": "Economy",
  "GroupPricing": false
},
"Status": "UpdatesComplete",
"Itineraries": [
  {
    "OutboundLegId": {
      "Id": "11235-1706052055--32356-0-13771-1706052210",
      "SegmentIds": [
        97
      ],
      "OriginStation": {
        "Id": 11235,
        "ParentId": 2343,
        "Code": "EDI",
        "Type": "Airport",
        "Name": "Edinburgh"
      },

But when I do console.log results before res.json, I get below results.

{ SessionKey: 'b85518fd1fa1484ba3c71862212ff32c_rrsqbjcb_06a13f0a788e803fcc56e78802891a26',
  Query: 
   { Country: 'GB',
 Currency: 'GBP',
 Locale: 'en-gb',
 Adults: 1,
 Children: 0,
 Infants: 0,
 OriginPlace: '11235',
 DestinationPlace: '4698',
 OutboundDate: '2017-06-05',
 InboundDate: '2017-06-06',
 LocationSchema: 'Default',
 CabinClass: 'Economy',
 GroupPricing: false },
  Status: 'UpdatesPending',
  Itineraries: 
   [ { OutboundLegId: '11235-1706052055--32356-0-13771-1706052210',
   InboundLegId: '16574-1706060900--32356-0-11235-1706061015',
   PricingOptions: [Object],
   BookingDetailsLink: [Object] },
 { OutboundLegId: '11235-1706052055--32356-0-13771-1706052210',
   InboundLegId: '16574-1706061615--32356-0-11235-1706061730',
   PricingOptions: [Object],
   BookingDetailsLink: [Object] },

As seen, the result contains ids and [object] instead of json array. I need to consume only some of the fields. So, I want create a new json array with selected fields before sending back my response. I am using fetch with then in my code.



via Prakash Bayas

No comments:

Post a Comment