I'm having trouble with an array and I cannot find what exactly is the problem.
First a function executes the following two lines in a loop:
varArray[overlapCounter] = [a, b, c];
overlapCounter++;
If I run console.log(varArray)
, the chrome log gives me the following:
[]
By opening the brackets this is the contents:
Array[0]
0: Array[6]
1: Array[6]
2: Array[6]
3: Array[6]
4: Array[6]
5: Array[6]
6: Array[6]
7: Array[6]
8: Array[6]
9: Array[6]
10: Array[6]
11: Array[6]
12: Array[6]
13: Array[6]
14: Array[6]
15: Array[6]
16: Array[6]
17: Array[6]
18: Array[6]
19: Array[6]
20: Array[6]
21: Array[6]
22: Array[6]
23: Array[6]
24: Array[6]
25: Array[6]
26: Array[6]
27: Array[6]
28: Array[6]
29: Array[6]
30: Array[6]
length: 31
__proto__: Array[0]
But if I run console.log(varArray.length)
, the value is 0
, and console.log(varArray[0])
returns undefined
.
Does anyone know what is going on there?
Full code:
$('#Salvar').click(function(e) {
e.preventDefault();
var dt_inicio_afastamento = $('#dt_inicio_afastamento').val();
var dt_fim_afastamento = $('#dt_fim_afastamento').val();
var observ_afastamento = $('#observ_afastamento').val();
var id_ocorrencia = $('#id_ocorrencia').val();
if ( dt_inicio_afastamento === "" || dt_fim_afastamento === "" || id_ocorrencia === ""){
return swal({
title: 'Todos os campos são obrigatórios!',
type: 'warning'
});
}
else{
function ajax2(names, dt_inicio_afastamento, dt_fim_afastamento, id_docente, observ_afastamento, id_ocorrencia){
$.ajax({
url: '../engine/controllers/afastamento.php',
data: {
id_afastamento : null,
dt_inicio_afastamento : dt_inicio_afastamento,
dt_fim_afastamento : dt_fim_afastamento,
observ_afastamento : observ_afastamento,
id_ocorrencia : id_ocorrencia,
id_docente : id_docente,
action: 'create'
},
error: function() {
swal({
title: 'Erro na conexão com o servidor',
text :'Tente novamente em alguns segundos',
type: 'error'
});
},
success: function(data) {
//console.log(data);
if(data === 'true'){
names.css( "background-color", "lightgreen" );
}
else{
names.css( "background-color", "lightcoral" );
}
}, //Sucesso Ajax2
type: 'POST'
}); //Ajax2
}
var overlapCounter = 0;
var varArray = [];
var requests = [];
$(".cada_docente").each(function(index) {
var names = $(this).prev();
var id_docente = $(this).val();
var nomeSerie = names.children('.nomeSerie').text();
var requests = Array();
(function(names, nomeSerie, dt_inicio_afastamento, dt_fim_afastamento, id_docente, observ_afastamento, id_ocorrencia){
requests.push($.ajax({
url: '../engine/controllers/afastamento.php',
type: 'POST',
data: {
id_afastamento : null,
dt_inicio_afastamento : dt_inicio_afastamento,
dt_fim_afastamento : dt_fim_afastamento,
observ_afastamento : observ_afastamento,
id_ocorrencia : id_ocorrencia,
id_docente : id_docente,
action: 'overlap'
},
success: function(data) {
if (data == "true"){ajax2(names, dt_inicio_afastamento, dt_fim_afastamento, id_docente, observ_afastamento, id_ocorrencia);}
else{
varArray[overlapCounter] = [names, dt_inicio_afastamento, dt_fim_afastamento, id_docente, observ_afastamento, id_ocorrencia];
overlapCounter++;
}
} //Sucesso Ajax1
})); //Ajax1
})(names, nomeSerie, dt_inicio_afastamento, dt_fim_afastamento, id_docente, observ_afastamento, id_ocorrencia); //Ajax1 Função
}); // Cada Docente
var defer = $.when.apply($, requests);
defer.done(function(){
console.log(varArray);
});
} //Else dados colocados
});