DEV Community

Discussion on: How to get data from an MySQL database in React Native

Collapse
 
servicelevel profile image
servicelevel

Hi, I also get this error:

$ node app.js
Go to localhost:3000/users so you can see the data.
/home/gg/dev/sb/API/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^

TypeError: Cannot read property 'query' of undefined
at /home/gg/dev/sb/API/app.js:25:16
at Handshake.onConnect (/home/gg/dev/sb/API/node_modules/mysql/lib/Pool.js:58:9)
at Handshake. (/home/gg/dev/sb/API/node_modules/mysql/lib/Connection.js:526:10)
at Handshake._callback (/home/gg/dev/sb/API/node_modules/mysql/lib/Connection.js:488:16)
at Handshake.Sequence.end (/home/gg/dev/sb/API/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
at Handshake.ErrorPacket (/home/gg/dev/sb/API/node_modules/mysql/lib/protocol/sequences/Handshake.js:125:8)
at Protocol._parsePacket (/home/gg/dev/sb/API/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/gg/dev/sb/API/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/gg/dev/sb/API/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/gg/dev/sb/API/node_modules/mysql/lib/protocol/Protocol.js:38:16)

the query method is undefined, but mysql module was installed with npm install mysql and is present

const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');

const connection = mysql.createPool({
host : 'localhost',
port : 3306,
user : 'mysql user',
password : 'mysql password',
database : 'db_name'
});

// Starting our app.
const app = express();
app.use(bodyParser.json({type: 'application/json'}));
app.use(bodyParser.urlencoded({ extended: true }));
// Creating a GET route that returns data from the 'users' table.
app.get('/users', function (req, res) {
// Connecting to the database.
connection.getConnection(function (err, connection) {

// Executing the MySQL query (select all data from the 'users' table).
connection.query('SELECT * FROM customers', function (error, results, fields) {
  // If some error occurs, we throw an error.
  if (error) throw error;

  // Getting the 'response' from the database and sending it to our route. This is were the data$
  res.send(results)
});
Enter fullscreen mode Exit fullscreen mode

});
});

// Starting our server.
app.listen(3000, () => {
console.log('Go to localhost:3000/users so you can see the data.');
});

when i go to localhost:3000/users I the get following message on my browser

This site can’t be reached localhost refused to connect.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

when I launch the script with node app.js the program listens to port 3000 and outputs:
Go to localhost:3000/users so you can see the data.

then listens on...

when i go to localhost:3000/users and get the connection refused message, I can THEN see the query undefined error on my terminal

I added the

app.use(bodyParser.json({type: 'application/json'}));
app.use(bodyParser.urlencoded({ extended: true }));

as proposed before in the comments but still nothing.. added
port : 3306, in the connection info and still nothing as well...
any idea?

Collapse
 
garmanbeau profile image
garmanbeau

Hey I was getting this problem too, and this is more for any other researchers later on. I was able to resolve the problem by uninstalling mysql (npm uninstall mysql), and then I used yarn (which is another packet manager, if you don't have it you'll have to do something like npm install --global yarn) and then I switched the code to require mysql2 and I was able to view the data in the browser. Using different packet managers in a project is not recommended, so you could probably use npm install mysql2, but I think the main solution here is to use the mysql2 package.