Skip to content Skip to sidebar Skip to footer

Typeerror: Cannot Read Property 'rows' Of Undefined

When following Getting started with Postgres in your React app, at the point where you process and export the getMerchants, createMerchant, and deleteMerchant functions, I am getti

Solution 1:

As per my comment:

Second parameter for query is the query parameters, and not a callback function. Pass in [], if you do not have parameters. Other than that, your code looks largely redundant. You should follow proper async pattern that library offers, and not re-create all those useless promises...

constgetMerchants = () => pool.query('SELECT * FROM userIds ORDER BY id ASC');

And then use it like this:

const {rows} = awaitgetMerchants();

Solution 2:

Right code, wrong password

The code itself is right! The error is thrown:

  1. if you have not set a password at all, or
  2. if you enter a wrong password. For example, I accidentally copied 'root' as a password from the guide, although I had set the password of my_user to 'postgres'. When everything else was already running and only the password was wrong, you get the same error.

The error in detail:

myuser@mypc:~/project/node-postgres$ node index.js

App running on port 3001.
/home/myuser/myproject/node-postgres/merchant_model.js:17
      resolve(results.rows);
                      ^

TypeError: Cannot read property 'rows' of undefined
    at pool.query (/home/myuser/myproject/node-postgres/merchant_model.js:17:23)
    at PendingItem.connect [as callback] (/home/myuser/myproject/node-postgres/node_modules/pg-pool/index.js:363:16)
    at Client.client.connect [as _connectionCallback] (/home/myuser/myproject/node-postgres/node_modules/pg-pool/index.js:246:23)
    at Client._handleErrorWhileConnecting (/home/myuser/myproject/node-postgres/node_modules/pg/lib/client.js:305:19)
    at Client._handleErrorMessage (/home/myuser/myproject/node-postgres/node_modules/pg/lib/client.js:325:19)
    at Connection.emit (events.js:198:13)
    at parse (/home/myuser/myproject/node-postgres/node_modules/pg/lib/connection.js:114:12)
    at Parser.parse (/home/myuser/myproject/node-postgres/node_modules/pg-protocol/dist/parser.js:40:17)
    at Socket.stream.on (/home/myuser/myproject/node-postgres/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (events.js:198:13)

I could find out the solution for this by following the guide at Getting started with Postgres in your React app: an end to end example where you also find more about the code that was used here. For digging deeper into this, How to quickly build an API using Node.js & PostgreSQL might help as well. To test this, you need to install PostgreSQL (or use a Webserver, but then, you will need SSL, see How to create a pool object in javascript that can connect to a database backend using SSL?) and create a "Merchant" table in the database as it is done in the guide.

As the files of the tutorial do not have any error, the problem of the question happens most probably (as in my case) because you have not fully followed the guide about how to set up the database role and password. You can check whether your settings are right by running, depending on whether you have chosen the guide's my_user or the standard postgres user to connect (if the latter, change the pool settings in merchant_model.js to postgres user):

psql -d my_database -U my_user

or:

psql -d my_database -U postgres

It should ask for a password:

Password for user postgres/my_user:

If your password tries are not accepted (standard test password is often postgres or the guide uses root), you probably do not have any password set, so easy. This was in my case, since the fresh install of PostgreSQL does not give the superuser postgres a password, neither has any other role that you create! It just did not catch my attention because I always used

sudo su postgres

to connect, which just asks you for the password of your Linux user, not that of the postgres user.

Thus: you must use a postgres (or my_user) user that has a password. You need to follow Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails in order to give postgres a password, and if you want to use another user like my_user, give it a password as well.

Only then, you get the connection done. And the error will disappear.

Outlook

When you have added the two files from the repository:

  • merchant_model.js (the one from the guide or the compact version above)
  • index.js (from the guide)

enter image description here

to the "node-postgres" directory, being in the project directory, you can run it with node index.js.

If that runs, you should see

App running on port 3001.

in the command prompt, with the command still running. Then you open a browser and enter localhost:3001 to see the backend:

enter image description here

When you then follow the guide and add the files from the repository:

  • App.js
  • index.js

enter image description here

to the "react-postgres\src" directory, you can run:

npm start

to see the frontend:

enter image description here

Try the Add button:

enter image description hereenter image description hereenter image description hereenter image description here


PS: Bad coding? Probably not!

The first answer is wrong in saying that the alleged "bad coding" is the cause of the error. You can use the full copies of the guide's github repository and everything will work, since the error comes from somthing else in the end. Yet, the code above may be bad coding, at least, count null entries in database column in a RESTfull way also uses the suggested await. I doubt that is that bad since it is working well, and when I later ran the code without the Promises on react-postgres, I vaguely remember having had a sort of message that you should use error handlers. Yet, for anyone interested, here is the advice of the first answer put into practice, it works, the "rows" error disappears, but that is just because the "rows" are not in the code anymore :))).

merchant_model.js:

constPool = require('pg').Poolconst pool = newPool({
  user: 'my_user',
  host: 'localhost',
  database: 'my_database',
  password: 'postgres',
  port: 5432,
});    

constgetMerchants = () => pool.query('SELECT * FROM Merchants ORDER BY id ASC') 

constcreateMerchant = (body) => {
    const { name, email } = body
    pool.query('INSERT INTO Merchants (name, email) VALUES ($1, $2) RETURNING *', [name, email])
}

constdeleteMerchant = () => {
    const id = parseInt(request.params.id)
    pool.query('DELETE FROM Merchants WHERE ID = $1', [id])
}

module.exports = {
  getMerchants,
  createMerchant,
  deleteMerchant,
}

Whether you use the code with or without Promises is up to you, perhaps one should rather stick to the guide and use it with Promises.

Post a Comment for "Typeerror: Cannot Read Property 'rows' Of Undefined"