简体   繁体   English

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

[英]request node get results from api response

I am making an API call using request . 我正在使用request进行API调用。

Everything is working ok, but I cannot actually get the results from the response. 一切正常,但是我实际上无法从响应中得到results

My code looks like this 我的代码看起来像这样

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);
    });

});

The res.send(body); res.send(body); looks like the below. 如下图所示。 Does anyone know how I can access the results ? 有谁知道我如何获得results I have tried body.results but this doesn't work. 我已经尝试过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":""
         }
      }
   }

So first you need to parse your body. 所以首先您需要分析您的身体。 Try this. 尝试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM