0

I am following this guide to query Node.js to Postgres

My code:

NodeJS:

  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`);
    c = db.getUsers(); 
    console.log(c);
  });

const getUsers = (request, response) => {
  pool.query('SELECT * FROM users', (error, results) => {
    if (error) {
      throw error
    }
     //console.log(results) do return data
     return results.rows[0].name //results not working either
  })
}

console.log(results) return

Result {
  command: 'SELECT',
  rowCount: 2,
  rows:
   [ { id: 1, name: 'Jerry', email: 'jerry@example.com' },
     { id: 2, name: 'George', email: 'george@example.com' } ],

}

When I npm run dev I get

> Ready on http://localhost:3000
undefined

SO any way so that I can get results data?

  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – cbr Mar 05 '20 at 08:32
  • Or this: [Returning a value from callback function in Nodejs](https://stackoverflow.com/q/23339907/996081) – cbr Mar 05 '20 at 08:33
  • The first one does not help. – Something new Mar 05 '20 at 08:37
  • The 2nd one give me `callback is not defined` – Something new Mar 05 '20 at 08:39
  • You also need to modify the way you call `getUsers` and its parameters. I also suggest reading: [What are callbacks?](https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/) – cbr Mar 05 '20 at 08:42
  • yeah seems like I forget that getUsers also it works. Thanks a lot. – Something new Mar 05 '20 at 08:47

0 Answers0