0

connection.query("SELECT email, password FROM authenticate where email=?", email,
    function (error, result) {
      if (error) {
        throw error;
      } else {
        if (result.length == 0) {
          console.log('name not available');
          res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
        } else if (result[0].password == password) {
          console.log(userData);
          connection.query("INSERT INTO userapplications SET ? ", userData,
            function (error, result, fields) {
              if (error){
                throw error;
              } 
              else{
                console.warn("insertion successful");
                res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
              }   
            });
        } else {
          console.log('invalid user and password');
          res.sendFile(path.join(__dirname + "/public/apply.html"))
        }
      }
    });
  connection.end();
error:

Cannot enqueue Query after invoking quit.

The database gets connected for some reason the data doesn't get added into the table. I'm a beginner.

James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

0

I think it's because of async nature of NodeJS. Try to concatenate the connection.end() with connection.query().

Like:

connection.query("your_code_here).end();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • I'm not getting the error now, but I don't see anything inserted into my MySQL table :( – HelloWorld Jul 14 '21 at 18:25
  • Actually it needs to be inside the callback function. Putting it there will still call it synchronously before the insertion. – Barmar Jul 14 '21 at 18:25
0

Move your connection.end(); inside callback like below.

connection.query("SELECT email, password FROM authenticate where email=?", email,
    function (error, result) {
      if (error) {
        connection.end();
        throw error;
      } else {
        if (result.length == 0) {
          console.log('name not available');
          res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
        } else if (result[0].password == password) {
          console.log(userData);
          connection.query("INSERT INTO userapplications SET ? ", userData,
            function (error, result, fields) {
              if (error){
                connection.end();
                throw error;
              } 
              else{
                console.warn("insertion successful");
                res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
                connection.end();
              }   
            });
        } else {
          console.log('invalid user and password');
          res.sendFile(path.join(__dirname + "/public/apply.html"))
          connection.end();
        }
      }
    });
  connection.end();

something like this, you can make changes.

Rohit Ambre
  • 921
  • 1
  • 11
  • 25