1

I'm trying to push the results of an SQL query to an array. However, it doesn't seem to be working. There was no solution online that I could find. If anyone could help me with this, I would greatly appreciate it.

        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
                            });
                        }
                    });
                }
            });
        }
    });
6
  • do you get any error during execution? Commented Sep 29, 2022 at 7:51
  • Nope. I get empty array Commented Sep 29, 2022 at 7:56
  • Have you tried to log the results of the queries? Commented Sep 29, 2022 at 8:12
  • when I log results inside query. It works fine. I'm not able to bring those results out of query. Commented Sep 29, 2022 at 8:17
  • okay, so count remain Undefined? have you tried using console.table it helps a lot with arrays and structures Commented Sep 29, 2022 at 8:22

1 Answer 1

2

You can try to use a single query to get data from MySql DB

select referer, hour(first_seen) h, count(*) n
from publisher
where date(first_seen) = curdate()
group by referer, hour(first_seen)
order by referer, hour(first_seen)

This way eliminating inner async method in js code.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi. Thank you. I now get error in console TypeError: Converting circular structure to JSON
let data = connection.query("SELECT referer, HOUR(first_seen) AS hour, COUNT(*) AS count FROM publisher WHERE date(first_seen) = CURDATE() GROUP BY referer, HOUR(first_seen)"); res.send(data);
Not sure about JS but my guess is you should use the .query method the way similar to OP, connection.query("SELECT ..", (error, results) => {..} and send result within a callback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.