请求节点从api响应中获取结果

时间:2017-05-01 12:29:43

标签: javascript node.js ajax xmlhttprequest

我正在使用request进行API调用。

一切正常,但我实际上无法从响应中获得results

我的代码看起来像这样

app.get('/events/:query', (req, res) => {

    request('http://www.skiddle.com/api/v1/events/search/?api_key=*****&keyword=elvis', (error, response, body) => {
        res.setHeader('Content-Type', 'application/json');
        res.send(body);
    });

});

res.send(body);如下所示。有谁知道我如何访问results?我尝试了body.results,但这不起作用。

{  
   "error":0,
   "totalcount":"25",
   "pagecount":20,
   "results":[  
      {  
         "id":"12958041",
         "EventCode":"FOOD",
         "eventname":"Elvis Tribute",
         "cancelled":"0",
         "venue":{  
            "id":72198,
            "name":"Simla Restaurant",
            "address":"5a Watling Street",
            "town":"Dordon",
            "postcode":"B78 1SS",
            "country":"GB",
            "phone":"00000000000",
            "latitude":52.597021,
            "longitude":-1.6113712,
            "type":"Restaurant",
            "rating":0
         },
         "imageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/2\/2\/a\/950766_0_elvis-tribute_th.jpg",
         "largeimageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/2\/2\/a\/950766_0_elvis-tribute.jpg",
         "link":"http:\/\/www.skiddle.com\/whats-on\/Birmingham\/Simla-Restaurant\/Elvis-Tribute\/12958041\/",
         "date":"2017-05-10",
         "startdate":"2017-05-10T19:00:00+00:00",
         "enddate":"2017-05-10T23:00:00+00:00",
         "description":"Elvis Tribute evening ",
         "openingtimes":{  
            "doorsopen":"19:00",
            "doorsclose":"23:00",
            "lastentry":""
         },
         "minage":"0",
         "imgoing":null,
         "goingtocount":"0",
         "tickets":false,
         "entryprice":"Ticket: GBP 26.95",
         "rep":{  
            "enabled":null
         }
      },
      {  
         "id":"12948859",
         "EventCode":"COMEDY",
         "eventname":"NCFComedy present The Elvis Dead - Part of Derby Comedy Fest",
         "cancelled":"0",
         "venue":{  
            "id":77454,
            "name":"Carnero Lounge",
            "address":"10 St Peter's Gate",
            "town":"Derby",
            "postcode":"DE1 1SH",
            "country":"GB",
            "phone":"",
            "latitude":52.9208847,
            "longitude":-1.4767019,
            "type":"bar",
            "rating":0
         },
         "imageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/5\/c\/c\/946899_2_ncfcomedy-present-the-elvis-dead-part-of-derby-comedy-fest_th.jpg",
         "largeimageurl":"https:\/\/d31fr2pwly4c4s.cloudfront.net\/5\/c\/c\/946899_2_ncfcomedy-present-the-elvis-dead-part-of-derby-comedy-fest.jpg",
         "link":"http:\/\/www.skiddle.com\/whats-on\/Derby\/Carnero-Lounge\/NCFComedy-present-The-Elvis-Dead---Part-of-Derby-Comedy-Fest\/12948859\/",
         "date":"2017-05-11",
         "startdate":"2017-05-11T19:00:00+00:00",
         "enddate":"2017-05-11T20:00:00+00:00",
         "description":"Rob Kemp: The Elvis Dead\r\nPart of Derby Comedy Festival",
         "openingtimes":{  
            "doorsopen":"19:00",
            "doorsclose":"20:00",
            "lastentry":""
         }
      }
   }

1 个答案:

答案 0 :(得分:0)

首先你需要解析你的身体。 试试这个。

try {
    body = JSON.parse(body);
}
catch(err) {
    console.log(err);
}
res.send(body.results);