Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

javascript - Node.js and mysql Callback : query in query callback

All I want to do is insert some data if my database doesn't have that, so I put Insert SQL into my callback function of my Select SQL, but I got error like this:

{ [Error: Cannot enqueue Query after invoking quit.] code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }

my code snippet is here:

db.query('SELECT count(*) as Resultcount FROM tablename WHERE email = ? and password = ?', [post.email, post.password], function(error, result){
    if (result[0].Resultcount == 0){
        var query2 = db.query('INSERT INTO tablename SET ?', [post], function(err, result) {
            if(err){
              console.log(err);
           }
             console.log(result);
          });
    }
    else{
        console.log('have data already');
    }
});

Could someone give me some advice? Thanks

----update----

actually, the callback function of select SQL is not an anonymous function, my code snippet about db.end() is like this:

var QueryResults = new queryResultFuntion(Back_results);

    db.query('SELECT count(*) as Resultcount FROM tablename WHERE email = ? and password = ?', [post.email, post.password], QueryResults.queryResult );


    db.end();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You db.end() call will queue the connection to close once the SELECT has completed, so when you attempt to do the inner INSERT query, the database connection will have been closed, hence the error PROTOCOL_ENQUEUE_AFTER_QUIT, as you are attempting to queue a new command after the connection is closed.

Depending on how you are creating the connection, you should either move your db.end() call inside the callbacks, or not have a db.end() call at all if the connection is opened on program start.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...