Why is my pg-query result throwing a TypeError

2.4k Views Asked by At

I am creating a node function to create a user in a PostgreSQL database. I'm using node.js, pg and pg-query to communicate from the app to the database.

Before I insert a new record Im trying to validate the email address does not already exist.

I am not sure why my result.rows[0].id is throwing a TypeError: Cannot read property '0' of undefined error Based on the documentation I am using the correct syntax to access the results of my query.

This is the function:

/* POST create user */
router.post('/adduser', function(req, res) {

    var salt = crypto.randomBytes(16);
    var newHash = hash(req.body.password, salt);

    query.connectionParameters = connString;
    query('SELECT id FROM users WHERE email = $1',[req.body.email], function(err, result) {
    if (err){
        console.log('Insert error: ' + err);
    }

    // If email exists, return error else insert new record
    if(result.rows[0].id != null) {
        query('INSERT INTO users (email, password, salt) VALUES ($1, $2, $3)', [req.body.email, newHash, salt], function(err, result) {
              if (err){
                console.log('Insert error: ' + err);
              }
              res.send( ( err === null ) ? { msg: '' } : { msg:err }  );
            });
    }
    else{
        console.log(result);
        res.send({msg:'User already exists'});
    }
    });
});

And the error I am receiving is as follows:

/home/robot/workspace/userapp/routes/users.js:35
    if(result.rows[0].id != null) {
                  ^
TypeError: Cannot read property '0' of undefined
    at /home/robot/workspace/userapp/routes/users.js:35:23
    at Function.onSuccess (/home/robot/workspace/userapp/node_modules/pg-query/index.js:55:7)
    at null.callback (/home/robot/workspace/userapp/node_modules/pg-query/node_modules/okay/index.js:7:16)
    at Query.handleReadyForQuery (/home/robot/workspace/userapp/node_modules/pg/lib/query.js:80:10)
    at null.<anonymous> (/home/robot/workspace/userapp/node_modules/pg/lib/client.js:158:19)
    at emit (events.js:129:20)
    at Socket.<anonymous> (/home/robot/workspace/userapp/node_modules/pg/lib/connection.js:109:12)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)

I'm new to javascript and node so it may be something very obvious. Here is the pg-query documentation if it helps.

** SOLUTION **

Changing the conditional to if(result[0].num == 0) { solved the problem. pg-query does not return a property called rows apparently.

2

There are 2 best solutions below

3
Jason On

It looks to me like the rows object is undefined to indicate that there are no users with the given email address. You could change the query to select count(*) as num from users where email = $1. This would ensure a result, and you can test if result.rows[0].num == 0.

1
ProfessionalAmateur On

** SOLUTION **

Changing the conditional to if(result[0].num == 0) { solved the problem. pg-query does not return a property called rows apparently.