S
S
shirokovea2017-07-31 12:03:53
JavaScript
shirokovea, 2017-07-31 12:03:53

How to output data to an array using node.js tedious?

Hello. I know that I asked a similar question, but I could not figure out the answer, so I immediately apologize for being stupid. Need a solution or an explanation on the fingers. There is a query to the database using the node.js tedious module. At the output, I want to get an array of data in order to insert part of the data into a new table in the future. The code itself:

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var ted_select = "query";

var config = {
    userName: 'name',
    password: 'pass',
    server: 'servername',
    options: {database : 'dbname'}
  };

function tedious_select(query) {
    var res = [];
    var connection = new Connection(config);

    var conn = connection.on('connect', function(err) {
      if (err) {
        console.log(err);
      } else {
          console.log('Connect database');
          request = new Request(query, function(err) {
              if (err) {
                  console.log(err);
              }
          });

          request.on('row', function(columns) {
              columns.forEach(function(column) {
                  if (column.isNull) {
                      res[column.metadata.colName] = null;
                  } else {
                      res[column.metadata.colName] = column.value;
                  }
              });
              console.log(res);
          });
          connection.execSql(request);  
          setTimeout(function(){connection.close(console.log('Disconnect database'));},10000);
      }
    });
}

var select = tedious_select(ted_select);
console.log(select);

Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question