Good to all I try to get the details of a foreign table after making a query, I do a for cycle to go through all the results of the main query and make a sub query with the data of the foreign id, this from a server in express using mysql as sgdb.
var sql = 'SELECT * FROM producto ORDER BY idproducto DESC LIMIT '+per_page+' OFFSET '+offset+'';
db.query(sql).then((results) =>{
productos = [];
if(results.length > 0){
for (var i = 0; i < results.length; i++) {
var item = results[i];
sql = 'SELECT * FROM categoria WHERE idcategoria = ?';
db.query(sql, item.categoria).then((result) => {
if(result.length > 0){
item.categoria = result;
}else{
item.categoria = null;
}
productos.push(item);
});
}
console.log(productos);
res.status(200).send({
productos: productos
});
}else{
res.status(404).send({
message: 'Sin registros'
});
}
});
According to the logs of the console, the data is ordered correctly, but when the subquery is finished and the data object is placed in another array to be sent by the server, they do not reach the array and the browser is empty.
Any ideas or comments? I have very little to be with javascript technologies and I just found the connection client with mysql. Thanks for your time