3
Hello,
When making an AJAX request, I have the following error:
Uncaught TypeError: Cannot read property of 'rede' undefined.
Until I understand the reason, I do not have the following value on account of the filter made by the user not return results when stamping the values within the code, but is it possible to treat this error? Like, put a 0
or something just so as not to interrupt reading the rest of my code?
This is my request, note the console.log trying to visualize the return and how the console returns the log:
$("#botao-filtrar").click(function(e){
e.preventDefault();
$.ajax({
url: 'datacenter/functions/filtraDashboardGeral.php',
async: true,
type: 'POST',
dataType: 'JSON',
data: {rede: $("#dropdown-parceria").val(), codLoja: $("#dropdown-loja").val(), mes: $("#dropdown-mes").val()},
success: function(data){
console.log(data[1]['rede']);
}
});
By default, data
is an array that has an extension 3
:
But sometimes, depending on the option selected by the user, it only returns one or two keys because it has no data for another key.
Following example:
And here is the PHP
:
<?php
session_start();
require_once('../../includes/gestaoOriginacao.php');
$rede = $_POST['rede'];
$codLoja = $_POST['codLoja'];
$mes = $_POST['mes'];
$nomeCompleto = $_SESSION['nomeCompleto'];
$dados = array();
$query = retornaQueryGrafico($rede, $codLoja, $mes, $nomeCompleto);
$resultado = mysqli_query($conexao, $query);
while($valores = mysqli_fetch_assoc($resultado)){
array_push($dados, $valores);
}
echo json_encode($dados);
function retornaQueryGrafico($rede, $codLoja, $mes, $nomeCompleto){
$hierarquia = $_SESSION['hierarquia'];
if($hierarquia == 1){
$query = "SELECT * FROM evolucao_originacao WHERE redeTratada = '{$rede}' and codLoja = '{$codLoja}' and mesReferencia = '{$mes}' and supervisor = '{$nomeCompleto}' order by mesReferencia";
} else {
$query = "SELECT * FROM evolucao_originacao WHERE redeTratada = '{$rede}' and codLoja = '{$codLoja}' and mesReferencia = '{$mes}' and supervisor = '9999999' order by mesReferencia";
}
return $query;
};
Put the value of
data
please.– BrTkCa
Instead of
console.log(data[0]['rede']);
, shouldn’t beconsole.log(data.rede)
?– Genos
Put the
datacenter/functions/filtraDashboardGeral.php
, the problem is in him.– Guilherme Nascimento
I tried to explain a little how comes the result of the request
– João Vitor
Edit the image and show open keys (or copy and paste the console here which is easier than image), so we know which is the return.
– Guilherme Nascimento
@Guilhermenascimento I edited with images and censored in some places on account of having confidential information. But I think it’s possible to visualize the structure of the object.
– João Vitor
Just do it
success: function(data){
 console.log(data[1]); 
 }
and see what returns.– Guilherme Nascimento
@Guilhermenascimento he returns a
undefined
– João Vitor
@jvbarsou does so
success: function(data){ console.log(data, data[1]); }
copy the result and put it in Pastebin.com and paste the link here (delete sensitive data such as phones)– Guilherme Nascimento
@Guilhermenascimento he returned that:
[] undefined
, without extension.– João Vitor
Returned
[]
is because the result came empty. Then you can do an if like thisif (data.length) { popula os dados aqui }
– Guilherme Nascimento