MSSQL - Nodejs - Undefined is not a function issue

2.6k Views Asked by At

I have been trying to connect to MSSQL database using node module in my protractor tests. I tried both the methods below but each time I am either getting undefined is not a function or getting cannot read property of "query/execute".

Error: [launcher] Running 1 instances of WebDriver
[launcher] Error: TypeError: undefined is not a function
    at exports.config.onPrepare (....\conf\Conf.js:39:28)

I have my connection defined in conf.js

   var HtmlReporter = require('protractor-html-screenshot-reporter');
  var mysql = require('../node_modules/mssql');


exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub', //desktop



  allScriptsTimeout: 40000,
  baseUrl: 'https://myurl.com/#/',

   // frameworks to use
    frameworks: 'jasmine',

//Capabilities to be passed to the webdriver instance.

  multiCapabilities: [{
    'browserName': 'chrome'
 // }, {
 //   'browserName': 'firefox'

 //},

 //   {
 //    'browserName': 'internet explorer'


  }],

 // Spec patterns are relative to the current working directory.
  specs: [
    '../tests/HomePage.js'
  ],


     onPrepare: function() {
    var sql = require('mssql/');
    var config = {
        server : '*****',
        user : 'myusername',
        password : 'pass',
        database: 'mydb'
    };



    browser.driver.manage().window().maximize();
    jasmine.getEnv().addReporter(new HtmlReporter({
        baseDirectory: '/MyDirectory/screenshots'
     }));



 },




  jasmineNodeOpts: {
    showColors: true
  }

};

In my Test, I did the following: test.js

    describe ( " page test , function() {


         it('Should add customer', function() {
            blah blah....

            });//This click will create a record in the backend

       // I am attempting to get the record:

       function runQuery() {

  var connection = new sql.Connection(config, function(err) {


      var request = new sql.Request(connection); // or: var request = connection.request(); 
      request.query('select top 1 as result from customers', function(err, recordset) {
        // ... error checks 
        console.log("this is the request: " + request);

        console.dir(recordset);
    });

   });

    };
1

There are 1 best solutions below

0
On BEST ANSWER

I used dqlserver unofficial and it worked pretty well. We use Windows authentication to login to MSSQL studio so use the below.

var sql = require('node-sqlserver-unofficial');
var conn_str = "Driver={SQL Server Native Client 11.0};Server={your server};Database={your database};Trusted_Connection={Yes}";

sql.open(conn_str, function (err, conn) {
    if (err) {
        console.log("Error opening the connection!");
        return;
    }
    conn.queryRaw("SELECT TOP 10 * FROM Employee", function (err, results) {
        if (err) {
            console.log("Error running query!");
            return;
        }
        for (var i = 0; i < results.rows.length; i++) {
            console.log("\n")
            console.log("Record Info: ");
            for (var j = 0; j < results.rows.length; j++) {
                console.log(results.rows[i][j]);
            }
        }
    });
});

Download MSSQL Unofficial package from here:

https://www.npmjs.com/package/node-sqlserver-unofficial