Node.js and mysql Callback : query in query callback

21,602

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.

Share:
21,602

Related videos on Youtube

Arvin
Author by

Arvin

http://about.me/ArvinH

Updated on April 22, 2020

Comments

  • Arvin
    Arvin about 4 years

    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();
    
    • loganfsmyth
      loganfsmyth about 11 years
      Are you closing your DB connection somewhere after this code?
    • Arvin
      Arvin about 11 years
      yes, when I finish db.query('select ...'), I called db.end();
    • loganfsmyth
      loganfsmyth about 11 years
      That means that by the time your INSERT query runs, the connection will be closed. Please add that code to your question and I'll make an answer.
    • loganfsmyth
      loganfsmyth about 11 years
      Also, when do you create the connection?
    • Arvin
      Arvin about 11 years
      my connection is create when the exports function be called
    • Arvin
      Arvin about 11 years
      I move my db.end() to my callback function of Insert SQL, and it works! It seems like I just didn't full understand how asynchronous works... but thank you anyway!
  • Lori
    Lori over 8 years
    Not doing db.end() at all results in having to terminate my script (which I'm starting on the command line with the node command) with Ctrl-C. But this did get rid of PROTOCOL_ENQUEUE_AFTER_QUIT
  • loganfsmyth
    loganfsmyth over 8 years
    @Lori Correct, that's why I prefaced it with Depending on how you are creating the connection. If you are writing a node server for instance, you'd only open the connection once, and there is no expectation that the server would every exit anyway. If you are writing a script, you'd want to end the connection once all of the operations in your script have completed.
  • Gel
    Gel about 2 years
    for me, I placed connection.end() or db.end() (in your case), outside of the result callback. As a result, it was ending the connection even before the callback has returned. Therefore the error is coming. So I just made sure its inside the connection.query or db.query block. connection.query(sql, (err,result)=> { if(err) throw err; console.log(result) connection.end(); })