0

the below nodejs code used for signin page in UI by connecting the MYSQL server previously the code was worked fine but in last two days it showing the error which I attached below. I tried various method of pooling connection but I can't rectify the issue. I hope somenone already overcame from the issue. Kindly please help me.

var mysql = require('mysql');
var express = require('express')
var cors = require('cors')

var app = express()

app.use(express.json())
app.use(cors())

app.post('/signin',(req,res)=>{
var pool  = mysql.createPool({
host     : '****************************',
user     : '*******',
password : '******',
database : '********',
multipleStatements:true,
timeout:60000
});
const sethu = {
    Password:req.body.Password,
    EmailAddress:req.body.EmailAddress
}

callbackWaitsForEmptyEventLoop = false;
    
pool.getConnection(function(err, connection) {  
  connection.query('select o.user_na,o.password,o.user_id,o.grp_id,o.is_admin,o.cli_id, case when 
o.master_cli_id =0 then "master" when o.master_cli_id <> 0 and o.is_admin=1 then "admin" when 
o.master_cli_id <> 0 and o.is_admin=0 then "user" end as user_type from (select 
c.master_cli_id,u.is_admin,u.cli_id,u.user_na,u.grp_id,u.user_id,cast(aes_decrypt(u.password,"
******" )as char(100)) as password from activedash_main.users u inner join 
activedash_main.clients c on u.cli_id = c.cli_id inner join activedash_main.admin a on u.cli_id = 
a.cli_id where u.user_na="'+sethu.EmailAddress+'" and u.password = 
aes_encrypt("'+sethu.Password+'","******"))as o;',function(err,results,fields){
  
if(err){
      res.send(err)
  }else{
      if(results.length<=0){
        res.send('Please check your mail-id and password')
       }else{
         if(results.length>1){
 pool.getConnection(function(err, connection) {  
 connection.query('select o.user_na,o.password,o.user_id,o.grp_id,o.is_admin,o.cli_id, 
 case when o.master_cli_id =0 then "master" when o.master_cli_id <> 0 and o.is_admin=1 then 
 "admin" when o.master_cli_id <> 0 and o.is_admin=0 then "user" end as user_type from (select 
 c.master_cli_id,u.is_admin,u.cli_id,u.user_na,u.grp_id,u.user_id,cast(aes_decrypt(u.password,
 "*******")as char(100)) as password from activedash_main.users u inner join 
 activedash_main.clients c on u.cli_id = c.cli_id inner join activedash_main.admin a on u.cli_id = 
 a.cli_id where u.user_na="'+sethu.EmailAddress+'" and u.password = 
 aes_encrypt("'+sethu.Password+'","*******") AND c.master_cli_id=0)as 
 o;',function(err,results,fields){
            if(err){
                res.send(err)
            }else{
                if(results.length<=0){
                  res.send('Please check your mail-id and password')
                 }else{
                   const length = results.length
                 
                if(sethu.EmailAddress===results[0].user_na){
                        if(sethu.Password===results[0].password){
      
                          const cli_id=results[0].cli_id
                          const grp_id=results[0].grp_id
                          const user_id=results[0].user_id
      
pool.getConnection(function(err, connection) {  
connection.query('insert into 
activedash_main.login(cli_id,user_id,grp_id,login_at)values('+cli_id+','+user_id+','+grp_id+',now())'
,function(err,result,field){
                             
 if(err){
                                  
 res.send('Please check your details')
 }else{
 res.send(results[0]);
connection.release();
 }
                      })
                      })                  
                    }else{
                        res.send('Password Incorrect')
                    }
                  }else{


                

         res.send('Please check your email-id')
                          }
//                      }
                    }
                  }
                  })
     

       })
             }else{
             const length = results.length
           
          if(sethu.EmailAddress===results[0].user_na){
                  if(sethu.Password===results[0].password){

                    const cli_id=results[0].cli_id
                    const grp_id=results[0].grp_id
                    const user_id=results[0].user_id

                    pool.getConnection(function(err, connection) {  
                    connection.query('insert into activedash_main.login(cli_id,user_id,grp_id,login_at)values('+cli_id+','+user_id+','+grp_id+',now())',function(err,result,field){
                        if(err){
                            res.send('Please check your details')
                        }else{
                            res.send(results[0]);
connection.release()
                        }
                    })
                    })                  
                  }else{
                      res.send('Password Incorrect')
                  }
                }else{
                    res.send('Please check your email-id')
                }
           }
          }
        }
        })
      })
})

app.listen(5000,()=>{
    console.log('Listening to 5000')
})

Error:

TypeError: Cannot read property 'query' of undefined
    at F:\Active-Dash\rough\sample.js:73:18
    at Handshake.onConnect (F:\Active-Dash\rough\node_modules\mysql\lib\Pool.js:58:9)
    at Handshake.<anonymous> (F:\Active-Dash\rough\node_modules\mysql\lib\Connection.js:526:10)
    at Handshake._callback (F:\Active-Dash\rough\node_modules\mysql\lib\Connection.js:488:16)
    at Handshake.Sequence.end (F:\Active-Dash\rough\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
    at F:\Active-Dash\rough\node_modules\mysql\lib\protocol\Protocol.js:404:18
    at Array.forEach (<anonymous>)
    at F:\Active-Dash\rough\node_modules\mysql\lib\protocol\Protocol.js:403:13
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
[nodemon] app crashed - waiting for file changes before starting...
2
  • It should be having problem getting the connection. You need to check for any err and only if no error you need to call connection.query in callback function of pool.getConnection. Can you log err in the callback of pool.getConnection and check if there is any error? Commented Sep 4, 2020 at 3:26
  • Thanks @ A Praveen Kumar for considering me... I tried as per you said the error it shows as below { "code": "PROTOCOL_SEQUENCE_TIMEOUT", "fatal": true, "timeout": 10000 } Commented Sep 4, 2020 at 3:32

2 Answers 2

1

So you are getting this error as connection is not being established. As a good you need to check for any err and only if no error you need to call connection.query in callback function of pool.getConnection.

In your case you are getting PROTOCOL_SEQUENCE_TIMEOUT. By default, this module sets a 10 second timeout to opening a new connection. If your server and network needs more than 10 seconds to establish the connection, you can adjust that with the connectTimeout option.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried using the connectTimeout:600000 option in the config but the issue remains same
Okay in your code sample given above you have timeout instead of connectTimeout, I assume you have used connectTimeout later. Even after increasing connectTimeout if you are still facing issue, try connection to mysql from mysql cli. If you get same issue with using mysql cli then issue would be with you mysql server configuration not your code.
1

Instead of adding connectTimeout try acquireTimeout it worked for me! Thanks.

var pool  = mysql.createPool({
    host     : 'courses.c7cbcmuhcbn1.us-east-1.rds.amazonaws.com',
    user     : 'wavicledev',
    password : 'wavicle1234',
    database : 'activedash_main',
    acquireTimeout:6000000
  }); 

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.