akash akash - 5 months ago 7
Node.js Question

why my node js is not getting connected to sql server?

I am trying to connect to sql server using node js. I have installed the package mssql and using sublime text editor.
this is my node js code.

var sql = require('mssql');

var dbConfig={
server:'CCI-LPT-21',
userName: 'sa',
password: 'sa123#',
port:1433,
options: {
instanceName: 'MSSQLSERVER2K12',
database: 'HRApp',
debug: {
packet: false,
payload: false,
token: false,
data: false
},
}
};

function getEmp()
{
//console.log(dbConfig);
var conn = new sql.Connection(dbConfig);
var req = new sql.Request(conn);

//console.log(conn)

conn.connect(function(err){
if(err){console.log(err);
return;
}
req.query("select * from Team",function(err,recordset){
if(err){
console.log(err);
return;
}
else
{
console.log(recordset);
}
conn.close();
})
})

}

getEmp();


when I run this from command promt,I get this errorenter image description here

when I did Console.log(dbconfig) it was having this data

enter image description here

Answer

Your error it's you have to set user and not userName.

Check this example from the doc:

var config = {
    user: '...',
    password: '...',
    server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
    database: '...',

    options: {
        encrypt: true // Use this if you're on Windows Azure
    }
}

It's user and not userName.