itaysk
itaysk

Reputation: 6246

node.js mssql - can't get simple sample to work

I am trying to run the simplest hello world with Node.js and the mssql package. https://www.npmjs.org/package/mssql

  1. I create a new folder, with an empty JS file (app.js)
  2. I copy and paste the sample from the mssql package page to the js file.
  3. I only change the config object with my DB connection settings.
  4. I run npm install mssql which is successful.
  5. I run node app.js

What happens is that the code doesn't get into the callback after creating a connection. So in the code below:

var connection = new sql.Connection(config, function(err) {
    alert(1);
    ...
    //more code...
});

I never get to the alert. No exceptions or errors either

I am probably missing something... Can you please help me spot it?

Update: I should mention that the DB is on Azure...

Upvotes: 0

Views: 1485

Answers (2)

itaysk
itaysk

Reputation: 6246

OK, after digging a bit in the docs for Tedious, I found out that if the DB is on Azure you must include options: {encrypt: true} in your configuration object. Now everything is working as expected.

Upvotes: 1

Dalorzo
Dalorzo

Reputation: 20014

Try this on your server side it works fine on my end:

var sql = require("mssql");

var dbConfig = {
    user:'sa',
    password:'password1',
    server:'serverName',
    database:'DBName'
};

var connection = new sql.Connection(dbConfig, function (err) {
   console.log(err);
   var request = new sql.Request(connection);

    request.query("Select 'Hello World' as var1", function (err, recordset, returnValue) {
       if (!err ){  
          console.log(recordset) ;      
        }else{
           console.log(err)
        }
    });

 });

Upvotes: 1

Related Questions