开发者问题收集

无法读取 nodejs 和 mysql 中未定义的属性“查询”

2020-09-04
2740

以下 nodejs 代码用于通过连接 MYSQL 服务器在 UI 中登录页面,之前代码运行良好,但在过去两天它显示了我在下面附加的错误。我尝试了各种池连接方法,但无法纠正该问题。我希望有人已经解决了这个问题。请帮助我。

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')
})

错误:

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个回答

因此,您收到此错误是因为未建立连接。作为一个很好的例子,您需要检查是否有任何错误,并且只有在没有错误的情况下,您才需要在pool.getConnection的回调函数中调用connection.query。

在您的情况下,您收到 PROTOCOL_SEQUENCE_TIMEOUT 。默认情况下,此模块设置10秒超时以打开新连接。如果您的服务器和网络需要超过10秒的时间来建立连接,您可以使用 connectTimeout 选项进行调整。

A Praveen Kumar
2020-09-04

我没有添加 connectTimeout,而是尝试使用 acquireTimeout,它对我有用!谢谢。

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