Node Mysql Cannot Enqueue a query after calling quit


Question

where do i close the mysql connection?

I need to run queries in sequence. I am writing code that looks like this at present:

var sqlFindMobile = "select * from user_mobiles where mobile=?";
var sqlNewUser = "insert into users (password) values (?)";
//var sqlUserId = "select last_insert_id() as user_id";
var sqlNewMobile = "insert into user_mobiles (user_id, mobile) values (?,?)";
connection.connect(function(err){});
var query = connection.query(sqlFindMobile, [req.body.mobile], function(err, results) {
    if(err) throw err;
    console.log("mobile query");
    if(results.length==0) {
        var query = connection.query(sqlNewUser, [req.body.password], function(err, results) {
            if(err) throw err;
            console.log("added user");
            var user_id = results.insertId;
            var query = connection.query(sqlNewMobile, [user_id, req.body.mobile], function(err, results) {
                if(err) throw err;
                console.log("added mobile");
                    //connection.end();
                });
        });
    }
});
//connection.end();

(I am a beginner with node, npm-express and npm-mysql. I have tried searching SO for "express mysql cannot enqueue" to find related questions and have not found them.)

1
24
1/15/2013 7:53:17 AM

I fixed this problem use this method.

connection.end() in your connection.query function

The fixed code is here

29
3/7/2013 9:11:45 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon