简体   繁体   English

节点js和mysql模块连接握手错误

[英]Node js and mysql module connection handshake error

I've been trying around getting this mess of libs in js working with, i've been using Codeigniter for years and now i came upfront with needing to use mysql in node, but it's very mess and tricky since the connections are being my main problem. 我一直在尝试让js中的这些库工作,我已经使用Codeigniter多年了,现在我开始需要在节点中使用mysql,但是由于连接是我的主要任务,所以非常麻烦和棘手问题。

So i'm making an adapter to make basic operations and using promise or not, it still gives me this error after making the first query. 因此,我正在制作一个适配器以进行基本操作并使用promise或不使用promise,在进行第一个查询后它仍然给我这个错误。

"Cannot enqueue Quit after invoking quit." 

Well usually we open con, query and close and by doing that or even not closing it throws always. 好吧,通常我们打开con,查询并关闭,通过这样做,甚至不关闭它总是抛出。 So my doubt is, what am i doing wrong or what do i need to add? 所以我的疑问是,我做错了什么或需要添加什么?

const mysql  = require('mysql')

let con = mysql.createConnection({
    host: "XXXXX",
    user: "XXX",
    password: "XXX",
    database: "XXX"
});;

con.on('error', function(err) {
    console.log("[mysql error]",err);
});

con.on('close', (err) => {
    console.log("Mysql closed connection");
})

/**
 * Executes a simple query with a given sql command and notifies when is complete
 * 
 * @param {string} sql
 * @param {function} callback
 * 
 * @return {object|json}
 */
executeSimpleQuery = (sql, args) =>
{
    return new Promise( (resolve, reject) => {
        con.connect( (err) => {
            con.query( sql, args, (err, rows) => {
                if (err) return reject(err);
                resolve(rows);
            })
        })
    })
}

/**
 * Closes a connection
 */
close = () =>
{
    return new Promise( ( resolve, reject ) => {
        con.end( err => {
            if ( err )
                return reject( err );
            resolve();
        } );
    } );
}

/**
 * Executes a simple query with a given sql command and notifies when is complete
 * 
 * @param {string} sql
 * @param {function} callback
 * 
 * @return {object|json}
 */
executeSimpleQueryx = (sql, callback) => 
{
    if (con.state !== "authenticated")
    {
        con.connect( (err) => {
            if (err) console.log("mysql error - " + err);
            console.log("Connection was been established in a simple query");
            con.query(sql, (err,result) => {
                if (err) throw err;
                callback(result);

            })
        });
    }
}

And i'm calling the function like this 我正在这样调用函数

adapter.executeSimpleQuery(`SELECT id from Users WHERE email= "${email}" LIMIT 1`)
        .then( rows => {
            console.log("We got row data--");
            console.log( JSON.stringify( rows ))
            callback(rows[0].id !== "undefined" ? rows[0].id : null)
        })
        .then( rows => {
            console.log("Closing db con..")
            return adapter.close()
        }, err => {
            return adapter.close().then( () => {throw err})
        })
        .catch( err => {
            console.log("EX rasise" + err)
        })
return adapter.close().then( () => {throw err})

Even if it closed correctly you are throwing an error. 即使它正确关闭,您也会抛出错误。 Print some error message first in success to check if this is the cause. 首先成功打印一些错误消息,以检查是否是原因。

Note: Better use pool connection on connect and release after each query. 注意:每次查询后,最好在连接和释放时使用池连接。 It will save overhead of creating and deleting connection and make queries work faster. 它将节省创建和删除连接的开销,并使查询工作更快。 CI in php internally manages that. PHP中的CI在内部进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM