5

node.jsでデータベース接続を閉じると、このエラーが発生します

Cannot enqueue Query after invoking quit.

これが私のコードです

socket.on('adminConnect', function (email) {
    connection = mysql.createConnection(db_config); // db_config has all details of database
    connection.connect(); // no problem in connection
    connection.query("SELECT id FROM user WHERE  email = '" + email + "' ", function (err, results, fields) {
        if (err) throw err;
        if (results[0]) {
            // Some code
            connection.end(); // its giving error "Cannot enqueue Query after invoking quit."
        } else {
            console.log("no id");
        }
    });
});
4

1 に答える 1

6

一般に、常に開いたり閉じたりするのではなく、接続を再利用します。

あなたの質問に答えるには、次のようにします。

connection.end();

接続を終了する前にすべてのクエリが完了する必要があるため、次のように安全であるため、コールバックの外に行を配置してみてください。

connection.query(.......);
connection.end();

コードは次のようになります。

socket.on('adminConnect', function (email) {
    connection = mysql.createConnection(db_config); // db_config has all details of database
    connection.connect(); // no problem in connection
    connection.query("SELECT id FROM user WHERE  email = '" + email + "' ", function (err, results, fields) {
        if (err) throw err;
        if (results[0]) {
            // Some code
        } else {
            console.log("no id");
        }
    });
    connection.end();
});
于 2013-10-24T10:59:51.840 に答える