0
const { Pool, Client } = require("pg");

const pool = new Pool({
  user: "postgres",
  host: "localhost",
  database: "postgres",
  password: ""
});


function one(){
    pool.query(
        "SELECT id, first_name as firstname, last_name as lastname FROM students_student",
        (err, res) => {
        let returnValue = JSON.stringify(res.rows);
        console.log(returnValue)
        return returnValue

        }
    )
}

var res = console.log(one())
console.log(res)

Just getting started with node and the pg library...

I don't understand why res is undefined in the console.log at the end of the above code (returnValue is fine). How should I be calling the function one()?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The duplicate question explains why `one()` returns `undefined` (which is, I think, your real question) but `res` is `undefined` because `console.log()` returns `undefined`. – Quentin Mar 13 '18 at 11:42
  • Yes, completely right - my issue was the async nature of the pg lib. Sort now - thanks. – Kevin Evans Mar 14 '18 at 12:11

0 Answers0