MSSQL - Nodejs - 未定义不是函数问题

时间:2015-06-07 21:29:45

标签: sql-server node.js protractor

我一直在尝试使用我的量角器测试中的节点模块连接到MSSQL数据库。我尝试了下面的两种方法,但每次我要么未定义不是函数或者无法读取"查询/执行"的属性。

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

我在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
  }

};

在我的测试中,我做了以下操作: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 个答案:

答案 0 :(得分:0)

我使用dqlserver非官方的,它工作得很好。我们使用Windows身份验证登录到MSSQL studio,因此请使用以下内容。

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]);
            }
        }
    });
});

从这里下载MSSQL Unofficial软件包:

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