1

i'm new to cheerio and want to scrape all team names in a particular table and return the data as json for instance like

{
    name: "Manchester City"
}

so far i've created below which is suppose to be an api returning the data, however i cant seem to access any particular elements? i keep getting following response TypeError: Converting circular structure to JSON

Code

app.get('/api/standings', function(req, res, next){

    var base = "http://www.skysports.com/football/competitions/premier-league/tables";
    var age = 2015;

    request.get(`${base}`, function(err, response, body) {

            var $ = cheerio.load(body);

            //get standings
            var classes = $('standing-table__table tbody tr').each(function()   {

                var d = $(this);
                var td = d.children('td.standing-table__cell standing-table__cell--name');
                return td.eq(0).text();
            });

            res.json(classes);

    });

});
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • Did you look up the error you are getting? This type of issue has been addressed a ton of times on stackoverflow already. – forgivenson Nov 07 '15 at 12:06
  • Possible duplicate of [Chrome sendrequest error: TypeError: Converting circular structure to JSON](http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json) – forgivenson Nov 07 '15 at 12:07
  • @forgiveson how is that even related. – Peter Pik Nov 07 '15 at 12:10
  • The error is telling you that your data has a circular reference. To convert it to JSON, you need to break that circle. – forgivenson Nov 07 '15 at 12:19

1 Answers1

2

First, you are using the incorrect cheerio function. You need .map not .each. Each just iterates without returning anything useful based on the code in the iterator function, whereas map collects everything returned by the iterator function into an array. So switch to .map and you should end up with an array of strings (assuming your selectors and HTML parsing is otherwise correct). You'll have ["Name 1", "Name 2", "Name 3"].

Then you need to convert that to your desired format which is an array of objects so you need another call to .map here.

res.json(classes.map(function (name) {return {name: name}}));
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274