I get this error when I refresh the page:
Uncaught TypeError: listaDeAutos.autos.foreach is not a function
I built the data here, which iterates and gets the data from the database and stores it in "salidaJson":
<?php
$salidaJson = '{"autos": [';
while ($row = $result->fetch_assoc()) {
$salidaJson = $salidaJson. '{';
$salidaJson = $salidaJson. '"idAuto":"' . $row['idAuto'] . '",';
$salidaJson = $salidaJson. '"Marca":"' . $row['Marca'] . '",';
$salidaJson = $salidaJson. '"Modelo":"' . $row['Modelo'] . '",';
$salidaJson = $salidaJson. '"idTipoDeVehiculo":"' . $row['idTipoDeVehiculo'] . '",';
$salidaJson = $salidaJson. '"FechaDeLanzamiento":"' . $row['FechaDeLanzamiento'] . '"';
$salidaJson = $salidaJson. '},';
}
$salidaJson = $salidaJson. '{"idAuto":"FIN", "Marca":"FIN","Modelo":"FIN","idTipoDeVehiculo":"FIN","FechaDeLanzamiento":"FIN"}';
$salidaJson = $salidaJson . ']}';
?>
Now this is the Javascript with the function that I'm trying to make, the objective is pretty simple, build a table with the data from the collection, I half wrote the table-building, but wanted to see if it was working at least the first part and I got the error:
<script>
var miTabla = $("#tableBody");
var listaDeAutos = <?php echo $salidaJson ?>;
console.log(listaDeAutos);
function cargaTabla() {
listaDeAutos.autos.foreach(function() {
var html = '<tr><td>'+ idAuto + '</td><td>' + Marca + '</td><td>'+ Modelo + '</td></tr>';
miTabla.append(html);
})
}
From what I understand forEach is used on Arrays but my data is an object because it contains "autos" that contains the arrays but I don't know how to make that function work with my object.
This is the data more clearly seen: