I've the next code on PHP:
[chat/verChatsUSUARIO]
$chats = [];
try {
$query = $this->db->connect()->query('SELECT * FROM chats_publicos_usuarios WHERE USUARIO_IP = :IP AND ID > :ID');
$query->execute([
'IP' => ! empty($datos['IP']) ? $datos['IP'] : NULL,
'ID' => ! empty($datos['ID']) ? $datos['ID'] : NULL,
]);
while ($row = $query->fetch()) {
$chats['chats'] = $row;
}
echo json_encode($chats);
}
Then, the next JQUERY code:
[chats.js]
idChat = 1
function verMisChats(direccionIP) {
$.ajax({
url: 'chat/verChatsUSUARIO',
type: 'POST',
data: {
ip: direccionIP,
id: idChat,
},
success: function(r) {
console.log(r)
if (r.chats) {
r.chats.forEach(chat => {
idChat = chat.ID
$("#sinChats").hide();
$("#misChats").append(verMiChat(chat));
})
}
verMisChats(direccionIP)
}
})
}
And I get this result when use that jquery function:
But when try foreach for every query result (in this example just 1 result), I get this error: