Jonathan Small
Jonathan Small

Reputation: 1079

Simple React with mySql fails to read table records

I created a React project by entering the following command prompt commands

mkdir mysql-test && cd mysql-test
npm init –y
npm install mysql –save

Then I created a file called app.js and I put in the following code:

    // app.js
       const mysql = require('mysql');

    // First you need to create a connection to the db
       const con = mysql.createConnection({
         host: 'rds-mysql-wmcw.cgz0te1dlkah.us-east-1.rds.amazonaws.com',
         user: 'masterUsername',
         password: 'masterPassword',
         database : 'db_wmcw'
       });

    //try to connect to the mySql database.  If an error occurs, display an 
      error message on the console

       con.connect((err) => {
       if(err){
          console.log('Error connecting to Db');
          return;
        }

    //a connection has been established to the database.  Now, run a query against the table 'homepage'
    //to retrieve each row and all columns of the table
  
       console.log('Connection established');
       con.query('SELECT * FROM homepage', (err,rows) => {
       //if an error occurs when reading the data from the table, display the error on the console
    
       if(err) {
         console.log(err);
         return;
       }
  
    //data has been retrieved from the table.  display the data to the console

       console.log('Data received from Db:\n');
       console.log(rows);
       });
       });
       con.end((err) => {
      // The connection is terminated gracefully
      // Ensures all previously enqueued queries are still
      // before sending a COM_QUIT packet to the MySQL server.
    });

Before I added the con.query command, when I typed in app.js at the command prompt, the response back was Connection established. So I know that I am connecting to the MySQL database. When I include the con.query command, and type in app.js at the command prompt, I get the following error:

Connection established { Error: Cannot enqueue Query after invoking quit. at Protocol._validateEnqueue (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\protocol\Protocol.js:203:16) at Protocol._enqueue (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\protocol\Protocol.js:138:13) at Connection.query (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\Connection.js:200:25) at Handshake.con.connect (C:\jrs\codercamp\mysql-test\app.js:21:7) at Handshake. (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\Connection.js:502:10) at Handshake._callback (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\Connection.js:468:16) at Handshake.Sequence.end (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24) at Handshake.Sequence.OkPacket (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\protocol\sequences\Sequence.js:92:8) at Protocol._parsePacket (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\protocol\Protocol.js:278:23) at Parser.write (C:\jrs\codercamp\mysql-test\node_modules\mysql\lib\protocol\Parser.js:76:12) code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }

Right at the top of the error, it states Cannot enqueue Query after invoking quit. I don't understand why the system thinks I am executing the query after invoking quit. I am not invoking quit unless that is what happens when I execute con.end but con.end is the last statement to execute. It is way after the query call.

Any assistance is greatly appreciated.

Thank you.

PS - I have included the host name, user ID, password, and database name to make debugging easier. There is no real important data in the database

Upvotes: 0

Views: 83

Answers (1)

Ukasha
Ukasha

Reputation: 2334

Okay, so the alternative solution would be uninstall mysql package and change with mysql2 package instead. As the author said:

MySQL2 is mostly API compatible with mysqljs and supports majority of features

This answer is for the questioner's comment on his post

If you really wanna reassign the one row of data, you can do:

const { product_detail, product_description } = results[0];

That's basic syntax :D (ES6 maybe)

Upvotes: 1

Related Questions