我正在尝试将 SQL 查询的结果推送到数组。但是,它似乎不起作用。我在网上找不到解决方案。如果有人能帮助我解决这个问题,我将不胜感激。
let data = []; connection.query("SELECT DISTINCT referer FROM publisher", (error, results) => { if (error) { console.log(error); } else { results.forEach(async (result) => { let referer = result.referer; let today = new Date(); for (let i = 0; i < 24; i++) { let hour = i; let start = new Date(today); start.setHours(hour, 0, 0, 0); let end = new Date(today); end.setHours(hour, 59, 59, 999); connection.query("SELECT COUNT(*) AS count FROM publisher WHERE referer = ? AND first_seen >= ? AND first_seen <= ?", [referer, start, end], (error, results) => { if (error) { console.log(error); } else { let count = results[0].count; data.push({ referer: referer, hour: hour, count: count }); } }); } }); } });
您可以尝试使用单个查询从MySql DB获取数据
这样就消除了 js 代码中的内部异步方法。