Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Js express mysql return json with subdnodes

I am trying to develop an API in node.js, express and mysql. I need return a json with subnodes/sub arrays. First make a query select in mysql, and this query return a array with my products. now i need itinerate this array (arrayProducts), get Id of current product and make other query select with this id in other table. When the second query return other array (arrayColors), and i make a for in this array and push each color in other array (arrayColorsAux) but when finish the second for (of second array) arrayColorsAux is emty. this is my code:

const express = require('express')
const routes = express.Router()
var mysql = require('mysql');
var pool = mysql.createPool({
    host: 'localhost',
    user: 'exampleUser',
    password: 'examplePass',
    database: 'exampleDb'
});

routes.get('/productos/cat/:id', (req, res) => {
    var id = req.params.id;
    pool.query('SELECT * FROM productos WHERE id_producto_categoria = ? and borrado=0', [id], 
    function (err, pro) {
        arryaPproductos = pro;
        for (let index = 0; index < arryaPproductos.length; index++) {
            var element = arryaPproductos[index];
            var idPro = element.Id;
            pool.query('SELECT * FROM productos_colores WHERE id_producto = ?', idPro, function (err, col) {
                element.colores = col;
                console.log("Array: ", arryaPproductos);
                //here show nice array

            });
        }
       //here return empty array
        res.json(arryaPproductos);
    });
})
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!