0

I am struggling with query.. I've been searching a lot, the only relevant posts I found had no answers.

row[0] returns undefined, I can't seem to find out why..

info:

As mentioned, logger.warn(rows[0]) returns undefined

rows.length returns 0

logger.warn(pool.escape(hash)) returns the correct hash(same as in DB)

logger.warn(rows[0].hash) returns undefined

This is the error in console/log file: TypeError: Cannot read property 'hash' of undefined

socket.on('hash', function(hash) {
        query('SELECT * FROM `members` WHERE `hash` = '+pool.escape(hash),
    function(err, rows) { 
            logger.warn(rows[0]) // returns "undefined"
            if((err) || (!rows.length)) return socket.disconnect();
    }

EDIT: QUERY FUNCITON

function query(sql, callback) {
if (typeof callback === 'undefined') {
    callback = function() {};
}
pool.getConnection(function(err, connection) {
    if(err) return callback(err);
    logger.info('DB Connection ID: '+connection.threadId);
    connection.query(sql, function(err, rows) {
        if(err) return callback(err);
        connection.release();
        return callback(null, rows);
    });
});

}

EDIT: Debug log

https://gyazo.com/858796b81c674ba816f974039eea1c09

EDIT: On disconnect

socket.on('disconnect', function() {
        logger.warn('Socket disconnect');
        io.sockets.emit('message', {
            type: 'logins',
            count: Object.size(io.sockets.connected)
        });
        delete users[user.email];
    })
12
  • what is hash? It seems like hash is undefined. Commented Jun 6, 2017 at 2:41
  • during broadcast of hash event,are u sending the hash? Commented Jun 6, 2017 at 2:42
  • Hash is defined, and returns the correct value. @Jiro90 EDIT: yes @RIYAJKHAN, I am, and that part is working fine. Commented Jun 6, 2017 at 2:43
  • What is query? Is hash numerical? Commented Jun 6, 2017 at 9:19
  • @robertklep, sorry, I didn't include that. Edited post now. Hash is an email address. [email protected] returns test%40mail.com Commented Jun 6, 2017 at 16:45

1 Answer 1

1

Looking at the log, it seems that the client is sending the hash event value like this: test%40mail.com.

However, as you state in your comment, in the database the value is stored as this: [email protected].

Which means that before running the query, you need to URI-unescape the hash value, and then SQL-escape it, before passing it to the database:

socket.on("hash", function(hash) {
  hash = pool.escape(decodeURIComponent(hash));
  query("SELECT * FROM `members` WHERE `hash` = " + hash, function(err, rows) {
    ...
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.