HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

My node app with valid syntax is failing silently, but not breaking. Infrastructure problem?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
problemappwithnodefailingbutinfrastructuresyntaxbreakingvalid

Problem

I built a simple Node app, and the code "works", with no failures, no syntax errors, and does not break, but one component doesn't work right. I showed a snippet on Stack Overflow, in this question, but viewers found no issue. So, I think the infrastructure, the method, in which I put this app together must be problematic, and I'd like to have it reviewed.

I put everything into a single source file to eliminate the headache of debugging whilst keeping track of modules.

Here is the code

Here's my relevant script:

var connection = createConnection();

connection.connect(function (err) {
    if (err) return callback(new Error('Failed to connect'), null);
    console.log('[Post]Connection with the officeball MySQL database opened...');

    connection.query(
        'INSERT INTO `officeball`.`sales_entries` SET category = ?, `group` = ?, date = ?, price = ?, customer = ?, seller = ?, commission = ?',
    salesData),

    function (err, rows, fields) {

        if (err) console.log(err);
        connection.destroy();
        console.log('[Post]...Connection with the officeball MySQL database closed.');

    }
});


And here's my console output:

Application initializing...
Application successfully initialized!
Server running at http://127.0.0.1:8080/
User username1 is attempting to validate for a hit...
Connection with the Officeball MySQL database openned...
...Connection with the Officeball MySQL database closed.
User username1 validated and is ready!
[Post] Connection with the officeball MySQL database opened...

Solution

From a once over:

-
Your console.log statement should be wrapped in a condition so that you can turn off all logging to the console. Node can really slow down because of logging to the console.

-
If you are going to log to the console, then it would make more sense to log your query and salesData as well.

-
Opening and closing a connection every time you want to post a sale is (very) bad practice, check out mysql.createPool

-
Calling connection.end(); will kill a connection gracefully. calling connection.destroy(), not so much..

All in all, I would always be sensitive to performance and established best practice.

Context

StackExchange Code Review Q#44541, answer score: 3

Revisions (0)

No revisions yet.