1

I am trying to select data from my database table and then print it in the console. whenever I try the console returns undefined.

const sql = await pool.query("SELECT * FROM users");
    for(rows in sql){
        console.log(rows.username)
    }
2
  • Have you read the documentation? for(rows in sql){ is not how to access the results of the query Commented Jul 7, 2022 at 19:45
  • Try logging sql to see what you're doing wrong Commented Jul 7, 2022 at 19:45

1 Answer 1

2

To get an array out of pool.query, you need to do the following:

const result = await pool.query("SELECT * FROM users");

const users = result.rows // get an array of users

If you want to get all the username from the users table, you can do:

const usernames = users.map((user) => user.username); // get an array of usernames
Sign up to request clarification or add additional context in comments.

Comments

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.