Select within a setInterval and fill an array in the nodejs

Asked

Viewed 130 times

0

I am making an application that needs to be checked in the bank if there has been any change using nodejs. The application works, however, when I have more than one record in the table, it duplicates the data I save in an array. For example: I have 2 records in the table, in the first loop of the setInterval, in the console.log() it shows me Array[2], in the second loop, Array[4] and so on. Does anyone know a way to build an array of data inside the setInterval that doesn’t duplicate? My code is like this:

var app = express();
var server = http.createServer(app);
var io = SocketIO.listen(server);

var Agenda = {};
var horarios = [];

setInterval(function(){

var query = connection.query('SELECT * FROM agenda WHERE id = 32');
query
.on('error', function(err) {
    console.log(err);
})
.on('result', function(ag) {
    Agenda = ag;
})
.on('end', function() {
})

var query2 = connection.query('SELECT * FROM horarios WHERE id_agenda = 32 ');
query2
.on('error', function(err) {
    console.log(err);
})
.on('result', function(hr) {        
    horarios.push(hr);
})
.on('end', function() { 
})    
Agenda.horario = horarios;
//console.log(JSON.stringify(Agenda));
if(horarios.length > 0){
    io.sockets.emit('time', {time: JSON.stringify(Agenda)});
}    

},3000);

1 answer

1


Problem solved, I just reset Time = [] after switching to socket.io

Browser other questions tagged

You are not signed in. Login or sign up in order to post.