title: Why am I getting undefined returned from my async/await Postgres query?
body: <p>I'm having trouble understanding where the <code>undefined</code> is coming from, during the following jest test. Here's the source file:</p>
  
  <pre><code>// UserAPI.js - file being tested
  const { Client } = require('pg')
  const dbConfig = require('../db')
  
  const findUserById = ({ userId }) =&gt; {
    const client = new Client(dbConfig)
    client.connect()
  
    return client
      .query('SELECT * FROM users WHERE id = $1', [userId])
      .then(result =&gt; {
        if (result.rowCount === 0) {
          return null
        }
        const foundUser = result.rows[0]
        if (foundUser) {
          const { password: storedpassword, ...user } = foundUser
          console.log(user)
          return user
        }
      })
      .then(() =&gt; client.end())
  }
  
  exports.byId = findUserById
  </code></pre>
  
  <p>Here's the test file:</p>
  
  <pre><code>// UserAPI.test.js - test file
  const api = require('../lib/UserAPI')
  const { Client } = require('pg')
  const SQL = require('sql-template-strings')
  const dbConfig = require('../db')
  const { clean_db, asyncForEach } = require('./helpers/dbTools')
  
  jest.setTimeout(1000)
  
  const defaultUsers = [
    {
      password: '12345678',
      username: 'smith@example.com',
      forename: 'John',
      surname: 'Smith',
      department: 'Accounts',
    },
    {
      password: '12345678',
      username: 'jones@example.com',
      forename: 'Bill',
      surname: 'Jones',
      department: 'Packing',
    },
  ]
  
  async function insertUsers(myUsersArray) {
    const client = new Client(dbConfig)
    client.connect()
    return asyncForEach(myUsersArray, async myUser =&gt; {
      let {
        password,
        username,
        forename,
        surname,
        department,
      } = myUser
      return client
        .query(
          SQL`INSERT INTO users (password, username, forename, surname, department)
          VALUES ( ${password}, ${username}, ${forename}, ${surname}, ${department})`,
        )
        .catch(err =&gt; console.error(err))
    }).then(() =&gt; client.end())
  }
  
  beforeAll(async () =&gt; {
    await clean_db('users')
    await insertUsers(defaultUsers)
  })
  
  afterAll(async () =&gt; {
    await clean_db('users')
    await insertUsers(defaultUsers)
  })
  
  describe('Querying the API for User data', () =&gt; {
    it('returns an individual user by id without password', async () =&gt; {
      const response = await api.byId({ userId: 1 })
      console.log('Response is: ', response)
      console.log('Response is of type: ', typeof response)
      console.log('Response properties: ', Object.keys(response))
      expect(response.username).toStrictEqual('smith@example.com')
      expect(response.id).toStrictEqual(1)
    })
  )
  </code></pre>
  
  <p>What I get when I run the test:</p>
  
  <pre><code> FAIL  tests/UserAPI.test.js
    ● Console
  
      console.log lib/UserAPI.js:43
        { id: 1,
          username: 'smith@example.com',
          created: 2020-01-02T16:29:19.221Z,
          modified: null,
          forename: 'John',
          surname: 'Smith',
          department: 'Accounting'
        }
      console.log tests/UserAPI.test.js:68
        Response is:  undefined
      console.log tests/UserAPI.test.js:69
        Response is of type:  undefined
  
    ● Querying the API for User data › returns an individual user by id without password
  
      TypeError: Cannot convert undefined or null to object
          at Function.keys (&lt;anonymous&gt;)
  
        68 |     console.log('Response is: ', response)
        69 |     console.log('Response is of type: ', typeof response)
      &gt; 70 |     console.log('Response properties: ', Object.keys(response))
           |                                                 ^
        71 |     expect(response.username).toStrictEqual('smith@example.com')
        72 |     expect(response.id).toStrictEqual(1)
        73 |   })
  
        at Object.it (tests/UserAPI.test.js:70:49)
  </code></pre>
  
  <p>What I'm failing to understand is how the <code>user</code> is being consoled-out from within the API.js file, but not being returned to the <code>const response = await api.byId({ userId: 1 })</code> line ? Given the console outputs are in the expected order, it's not an async issue, right?</p>