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