Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
How to make this variable execture after i get the data?

I don't know what the problem is so, i know the title doesnot describes anything my appologies for that. Here is the problem I am facing. I am currently working in a MERN app.

I have a route in express where i request for some data from the database from two tables.

playlist.post("/getplaylistSongs", (req, res) => {
  const { cookie } = req.body;
  const sql = "SELECT * FROM playlist WHERE user_id = ?";
  dbCon.query(sql, [cookie], (err, result) => {
    if (err) {
      res.status(400).json({
        message: "failed to get playlist details ",
      });
    } else {
      let playlistSongs = [];
      result.forEach((element, index) => {
        const audio_id = element.audio_id;
        dbCon.query(
          "SELECT * FROM audios where video_id = ?",
          [audio_id],
          (err, song) => {
            if (err) {
              res.status(400).json({
                message: "failed to get music",
              });
            } else {
              playlistSongs.push(song);
            }
          });
      });
     console.log(playlistSongs); 

      res.status(200).json({
        playlist: playlistSongs,
      });
    }
  });
});

but the problem here is however i try to do it i always get the playlistSongs as an empty array. thank you. comment for correcting the question.

EDIT : Now i know the problem is realted to execution of the javascript.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try with async / await:

playlist.post('/getplaylistSongs', async (req, res) => {
  const { cookie } = req.body;
  const sql = 'SELECT * FROM playlist WHERE user_id = ?';
  try {
    const result = await dbCon.query(sql, [cookie]);

    let playlistSongs = [];
    result.forEach(async (element, index) => {
      const audio_id = element.audio_id;
      const song = await dbCon.query(
        'SELECT * FROM audios where video_id = ?',
        [audio_id]
      );
      playlistSongs.push(song);
    });
    console.log(playlistSongs);

    res.status(200).json({
      playlist: playlistSongs,
    });
  } catch (e) {
    res.status(400).json({
      message: 'failed to get playlist details ',
    });
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda