No Node.js Access To MySQL Community Database . . .
Posted by:
Kon Tiki
Date: November 22, 2021 07:25AM
My machine runs:
Ubuntu Linux 20.04.2,
Node 14.15.1,
MySQL Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
I am having major problems accessing MySQL database from a simple Node.js web app. I am trying to use a development version of the web app on the local machine to connect to a local instance of the web app's database.
I have successfully used MySQL Workbench to make a connection to the local MySQL database and this works fine - I use it to edit the database.
The first action by web app users is basic authentication, e.g. registration/login using a small form on the home page of the website.
All modules in the web app execute as intended until the query to check a user name in the app's database is sent to a module which queries the database, mysqldb.js.
This module requires an npm module called mysql which makes the actual connection to the MySQL database, runs the query through to it and returns three parameters: error (JS error object), result (table of DB query results) and fields (the column heads of the result table).
I attach the source code of the module below:
const mysql = require('mysql');
/** Queries the app's MySQL database
* @param {Object} query - the query presented to the MySQL DB
* @param {Object} dbcallback - the callback function applied to the query response
* */
const queryNodeAppData = (query, dbcallback) =>
{
console.log("In MySQL Query script now ...")
console.log("Query: " + query);
const conn = mysql.createConnection(
{
socketPath: "/var/run/mysqld/mysqld.sock",
database: "nodeapp",
user: "root",
password: "MY.$qu3@13r"
});
console.log("Connection user: " + conn.user);
conn.connect(query, (errconn, result, fields) =>
{
if (errconn)
{
console.error('Error connecting: ' + errconn.stack);
dbCallback(errconn, null, null);
}
else
{
console.log('Connected as id ' + connection.threadId);
dbCallback(null, result, fields);
}
});
};
module.exports = { queryNodeAppData };
The console output for this app is below.
The app does NOT execute any of the console logs within the MySQL query statement so we don't even know if any connection has been made - I suspect not.
$ nodemon app
[nodemon] 2.0.14
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
HTTP Server for NodeApp started at port 3000
HTTPS Server for NodeApp started at port 3001
We're in to the back end of NodeApp !
Someone has POSTed data to the Node server ...
URL trimmed endpoint: reg-user
Input Value: tamjk
In router.js now ...
Endpoint: reg-user Data: tamjk
Handler: authen.js
In handler authen.js now ...
Mode: reg Field: user Value: tamjk
In format validation now ...
Format Validation Message: Valid format
Connecting to user-data database checking user ...
In MySQL Query script now ...
Query: SELECT user FROM users WHERE user = 'tamjk'
Connection user: undefined
Router returned message undefined
Callback Message: undefined
Message: undefined
res: [object Object]
Sent back response: undefined
_http_outgoing.js:696
throw new ERR_INVALID_ARG_TYPE('first argument',
^
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
at write_ (_http_outgoing.js:696:11)
at ServerResponse.write (_http_outgoing.js:661:15)
at reqCallback (/home/sandbar/Desktop/nodeapp-local/app.js:113:6)
at IncomingMessage.<anonymous> (/home/sandbar/Desktop/nodeapp-local/app.js:81:10)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ERR_INVALID_ARG_TYPE'
}
[nodemon] app crashed - waiting for file changes before starting...