Babu
Babu

Reputation: 21

node.js is not connecting to SQL Server database using SQL Server authentication

I'm using node.js and the mssql package to connect to a SQL Server database using SQL Server authentication. When I try connecting using SQL Server Management Studio with the same credentials, it is working fine. However, with node.js, I cannot login and get an error code ELOGIN with connection error.

I've tried many examples shown in google and I'm facing the same issue. Let me know what I'm missing. Here is the code snippet of mine.

Code starts here

var sql = require('mssql');
var config = {
  server: 'scaXXXXXXXXXXXX',
  database: 'scaXXXXXXXXXX',
  user: 'svcXXXXXXX',
  password: 'Password',
  port: 1433
};

function listProducts() {
  var conn = new sql.ConnectionPool(config);
  conn.connect().then(function () {
  var request = new sql.Request(conn);
  request.query("select top 1 * from dbo.Persons").then(function 
  (recordSet) {
        console.log(recordSet);
        conn.close();
    }).catch(function (err) {
        console.log(err);
        conn.close();
    });
   }).catch(function (err) {
    console.log(err);
   });
 }

listProducts();

This is the error while running this code:

ConnectionError: Login failed for user 'svcXXXXXXX'.

at Connection.tedious.once.err (C:\aws\node_modules\mssql\lib\tedious.js:244:17)
at Object.onceWrapper (events.js:277:13)
at Connection.emit (events.js:189:13)
at Connection.processLogin7Response (C:\aws\node_modules\tedious\lib\connection.js:1397:14)
at Connection.message (C:\aws\node_modules\tedious\lib\connection.js:1932:14)
at Connection.dispatchEvent (C:\aws\node_modules\tedious\lib\connection.js:1084:36)
at MessageIO.messageIo.on (C:\aws\node_modules\tedious\lib\connection.js:984:14)
at MessageIO.emit (events.js:189:13)
at Message.message.on (C:\aws\node_modules\tedious\lib\message-io.js:32:14)
at Message.emit (events.js:194:15)

code: 'ELOGIN',
originalError: { ConnectionError: Login failed for user 'svcXXXXXXX'.
at ConnectionError (C:\aws\node_modules\tedious\lib\errors.js:13:12)
at Parser.tokenStreamParser.on.token (C:\aws\node_modules\tedious\lib\connection.js:735:29)
at Parser.emit (events.js:189:13)
at Parser.parser.on.token (C:\aws\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
at Parser.emit (events.js:189:13)
at addChunk (C:\aws\node_modules\readable-stream\lib_stream_readable.js:297:12)
at readableAddChunk (C:\aws\node_modules\readable-stream\lib_stream_readable.js:279:11)
at Parser.Readable.push (C:\aws\node_modules\readable-stream\lib_stream_readable.js:240:10)
at Parser.Transform.push (C:\aws\node_modules\readable-stream\lib_stream_transform.js:139:32)
at doneParsing (C:\aws\node_modules\tedious\lib\token\stream-parser.js:80:14)

message: 'Login failed for user \'svcXXXXXXX\'.',
code: 'ELOGIN' }, name: 'ConnectionError' }

I expect one record from database should extract and display.

Upvotes: 2

Views: 1429

Answers (2)

pabloescobar
pabloescobar

Reputation: 11

Try this out. It worked for me. If you are not doing with a localhost Database you need to be in that network. Make sure you can ping the database server.

var sql = require("mssql");
var moment = require("moment");
let port = process.env.PORT;
if (port == null || port == "") {
    port = 8000;
}

var config = {
    user: "xxxx",
    password: "xxxxx",
    server: "xxxxxx",
    database: "xxxx"
};

const dbconn = sql.connect(config, err => {
    if (!err) {
        console.log("Connected to the database");
    } else {
        console.log("Problem in connecting to database");
        console.log(err);
        console.log("testing ");
    }
});

app.get("/getSummaryDetails", (req, res) => {
    dbconn.query("exec QCGrid", (err, rows, fields) => {
        if (!err) {
            res.send(rows.recordsets[0]);
        }
    });
});

Upvotes: 0

Namhoon Lee
Namhoon Lee

Reputation: 178

it looks your login information is not correct. did you write proper user name and password?

if your login info is correct, then check out login info has authority to be connected from the external environment

Upvotes: 0

Related Questions