Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda