'TypeError: Cannot read properties of undefined (reading 'rows')

Trying to use Express JS with React app and PostgreSQL DB for first time, I am following this tutorial, but editing slightly for my need (recipe storage). I am currently getting this error and not sure why...

TypeError: Cannot read properties of undefined (reading 'rows')
    at /Users/xx/Desktop/Programming/recipes/recipes/backend/node_postgres/recipe_model.js:16:23
    at Query.callback (/Users/xx/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/pg-pool/index.js:415:18)
    at Query.handleError (/Users/xx/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/pg/lib/query.js:128:19)
    at Client._handleErrorMessage (/Users/xx/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/pg/lib/client.js:335:17)
    at Connection.emit (node:events:526:28)
    at /Users/xx/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/pg/lib/connection.js:114:12
    at Parser.parse (/Users/xx/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/pg-protocol/dist/parser.js:40:17)
    at Socket.<anonymous> (/Users/xx/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (node:events:526:28)
    at addChunk (node:internal/streams/readable:315:12)

The relevant code from recipe_model.js

const getRecipe = () => {
  return new Promise(function (resolve, reject) {
    pool.query('SELECT * FROM recipes ORDER BY id ASC', (error, results) => {
      if (error) {
        reject(error)
      }
      resolve(results.rows)
    })
  })
}

Thanks for any assistance.



Solution 1:[1]

Your query is working fine but rows does't exist in result simple resolve result only and check by console.log what you are getting in result.

Reply me in comment for any query.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Momin Raza