我正在尝试在循环内的快速查询内的数组中添加一些值,我想在所述循环外获取该数组。
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
}
);
});
});
首先打印“ 22222”,然后打印“ 11111”,反之亦然