S
S
Semyon Kataparov2016-05-03 17:44:25
JavaScript
Semyon Kataparov, 2016-05-03 17:44:25

Why is only 1 link displayed as a result, and not all?

There is a code:

io.sockets.on('connection', function(socket) {
  var group = 'fnatic';
  var url = 'http://steamcommunity.com/groups/' + group + '#members';
  request(url, function(err, resp, body){
    $ = cheerio.load(body);
    links = $('a');
    $(links).each(function(i, link){
      // console.log($(link).attr('href'));
      var users = $(link).attr('href');
      socket.emit('hatler', users);
    });
  });

});

If output to the console, then all links will be displayed, if passed to the client via socket.io, then the most recent link will be displayed.
Result processing code in the client side:
var socket = io.connect('http://localhost:3000');
      socket.on('hatler', function(users) {
        $('.result').html(users);
      });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-05-03
@truexizt1

You send each user as a separate message, collect them in an array and send this array after .each()
UPD

io.sockets.on('connection', function(socket) {
  var group = 'fnatic';
  var url = 'http://steamcommunity.com/groups/' + group + '#members';
  request(url, function(err, resp, body){
    $ = cheerio.load(body);
    links = $('a');
    var users = [];
    $(links).each(function(i, link){
      // console.log($(link).attr('href'));
      users.push($(link).attr('href'));
    });
    socket.emit('hatler', users);
  });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question