简体   繁体   中英

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!

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

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

For google viewers...

If the above solution doesn't work for you. You should check the SQL Server Browser in Computer services is running as this is what processes the requests to the server.

That one stumped me for a while

将 dbConfig 的“用户名”更改为“用户”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM