0

I'm trying to add some value in an array within an express query within a loop, and I want to get that array outside of the said loop.

app.get("/getMatchedProfile",function (req,res) {
   con.query("SELECT\n" +
       "\tidUser2\n" +
       "FROM\n" +
       "\t`match`\n" +
       "WHERE\n" +
       "\tidUser1 = ?", [req.query.id], function (error, results) {
      if (error) throw error;
      let taille = results.length;
      let arrayPromise = [];//array of queries
      let arrayMatch = [];//the array i want to retreive
      console.log(taille);
      for (let i = 0; i < taille; i++) {
         arrayPromise.push(con.query("SELECT idUser1, idUser2 FROM `match` WHERE idUser1 = ?", [results[i].idUser2],
             function (error, results) {
                if (error) throw error;
                for (let j = 0; j < results.length; j++) {
                   if (results[j].idUser2 == req.query.id) {
                      arrayMatch.push(results[j].idUser1);
                      console.log("1111 : "+arrayMatch)//should print first
                   }
                }
             }
         )
         )
      }
      Promise.all(arrayPromise).then(function() {
           console.log("22222 : " + arrayMatch);//should print 2nd + not empty
          }
      );
   });

});

Print "22222" first and "11111" second, where it should be the other way around

Cheesus
  • 13
  • 5

0 Answers0