I'm making an application that needs to be checking in the database if there was any change using nodejs. The application works, however, when I have more than one record in the table, it gets duplicate data that I keep in an array. For example: I have 2 records in the table, in the first loop of setInterval, in 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 does not duplicate? My code looks 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);