Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs mssql ConnectionError: Login failed for user ' '

This is my code:

var sql = require("mssql");

var dbConfig = {
server: "server",
username: "user",
password: "password",
database: "database"
};

function getEmp() {
var conn = new sql.Connection(dbConfig);
console.log(conn);
var req = new sql.Request(conn);
conn.connect(function (err) {
    if (err) {
        console.log(err);
        return;
    }
    req.query("SELECT * FROM Alg.User", function (err, recordset) {
        if (err) {
            console.log(err);
            return;
        }
        else {
            console.log(recordset);
        }
        conn.close();
    });
});
}

getEmp();

And this is the error I'm logging:

 { ConnectionError: Login failed for user ''.
at Connection.<anonymous> (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\mssql\lib\tedious.js:378:25)
at Connection.g (events.js:291:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.processLogin7Response (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:672:16)
at Connection.message (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:1082:21)
at Connection.dispatchEvent (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:519:45)
at MessageIO.<anonymous> (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:439:23)
at emitNone (events.js:86:13)
at MessageIO.emit (events.js:185:7)
name: 'ConnectionError',
message: 'Login failed for user \'\'.',
code: 'ELOGIN' }

Anyone knows what I'm doing wrong? It looks like my variable username isn't put in the connectionstring? But I have no idea why...

Thanks in advance!

Solution:

The problem was that in dbConfig, the username variable should be changed to user! And the sql query was wrong also, it should have been [Alg].[User], because 'User' is a keyword!

like image 430
Milan_w Avatar asked Feb 23 '17 12:02

Milan_w


2 Answers

Using tedious / msnodesqlv8 driver for Windows worked for me:

Instead of:

var sql = require("mssql");

I changed it to:

var sql = require("mssql/msnodesqlv8");

I did not hard code user name/password.

Here's what my dbconfig looks like:

var db_config = {
  driver: "msnodesqlv8",
  server: "ServerName",
  database: "databaseName",
  options: {
    trustedConnection: true,
    useUTC: true
  }
}
like image 187
shahreen Avatar answered Oct 12 '22 13:10

shahreen


Please make "username" to "user" in your configuration. If you try to console.log(conn), you will see that configuration is using "user" instead of userName.

I have answered this question, please refer to SQL Server Error "[ConnectionError: Login failed for user '****'.] "while connecting SQL Server with Nodejs using MSSQL

like image 45
drink-a-Beer Avatar answered Oct 12 '22 11:10

drink-a-Beer