0

I have a console program where the user scans in serial numbers, and those serial numbers get added to a database.

const mysql = require('mysql2');
const read = require('readline-sync');

const conn = new mysql.createConnection(config);

conn.connect(
function(err){
    if(err){
        throw err;
    }
    else{
        console.log("Connection Established");
        while(1){
            var sn = read.question('Scan in serial number: ');
            conn.query('INSERT INTO test (serial) VALUES (?);',
            [sn], function(err, results, fields){
                if (err){
                    throw err;
                }
                else{
                    console.log("Added stuff");
                }
            });
        }
    }
}
);

When the code runs it successfully connects to the database but queries the database. It continually prompts for user input.

Alternatively, I tried storing serial numbers in an array and then loops through it adding each element, like this.

const mysql = require('mysql2');
const read = require('readline-sync');

var array = [];
var sn = " ";
while (1) {
    sn = read.question('Scan in serial number, or enter "done" if finished scanning');
    if (sn == "done") {
        break;
    }
    array.push(sn);
}

conn.connect(
    function (err) {
        if (err) {
            throw err;
        }
        else {
            console.log("Connection Established");
            array.forEach(function (sn) {
                conn.query('INSERT INTO test (serial) VALUES (?);',
                    [sn], function (err, results, fields) {
                        if (err) {
                            throw err;
                        }
                        else {
                            console.log("Added stuff");
                        }
                    });
            });
        }
    }
);

In this case, it works inconsistently. Sometimes it works fine, and other times it fails to connect and throws a timeout error. Is there a better way to accomplish this and/or am I doing something wrong?

1
  • If you try to concatenate all queries and then execute a single string. Commented Jan 15, 2019 at 20:16

2 Answers 2

1
var promises = []
function dbOp(value) {
  return new Promise(function(resolve, reject) {
      conn.query('INSERT INTO test (serial) VALUES (?);',
                [value], function (err, results, fields) {
                    if (err) {
                        return reject(err)
                    }
                    else {
                        console.log("Added stuff"); 
                        resolve(results)
                    }
  }
}




conn.connect(function(err){
    if(err){
        throw err;
    }
    else{
        for (i = 0; i < array.length; ++i) {
            promises.push(dbOp(array[i]));
        }
    }
});

  Promise.all(promises)
      .then((results) => {
        console.log("done", results);
      })
      .catch((e) => {
          console.log(e)
      });
Sign up to request clarification or add additional context in comments.

4 Comments

It behaves exactly the same way as before.
@ajjohnson190 oh , I looked it too quick and thought it was a promise , so basically you need to wrap your callback with native promise and then loop through array push into promise chain.
That works, if you don't mind could you explain what that does? I don't quite understand.
@ajjohnson190 so to handle db operation , you are using .query which is a callback. I don't know if you can use async/await so I wrapped your callback in a native promise which is another way to handle async operation and then push into a Promise.all which resolves all the promise you add from loop through the array. if you need more please google callback, promise, async/await ways to handle async operations.
0

This might be caused by short idle timeout setting in your mysql server. client.connect() is pretty much a no-op in mysql2, it connects immediately when you call mysql.createConnection(). You can change order to establish connection only after all data is collected:

const mysql = require('mysql2');
const read = require('readline-sync');

var array = [];
var sn = ' ';
while (1) {
  sn = read.question('Scan in serial number, or enter "done" if finished scanning');
  if (sn == 'done') {
    const conn = mysql.createConnection(config);
    array.forEach(function(sn) {
      conn.query('INSERT INTO test (serial) VALUES (?);', [sn], function(err, results, fields) {
        if (err) {
          throw err;
        } else {
          console.log('Added stuff');
        }
      });
    });
  }
  array.push(sn);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.