Connect to a Microsoft SQL database using Node.JS and mssql/tedious

558 views Asked by At

I am trying to connect to a SQL2019 database using a Node.JS script. The server uses Windows Authentication to logon.

I successfully connected to it and pulled all users from the table by requiring mssql/msnodesqlv8 and installing Microsoft SQL Server 2012 Native Client, but for obvious reasons i would rather use Tedious as the driver to avoid having to manually install anything.

My current code is as below

var sql = require('mssql');
const config = {
  server: 'SERVERNAME',
  database: 'DATABASE',
  options: { trustedConnection: true, trustServerCertificate: true }
};


sql.connect(config, (err) => {
  if (err) {
    console.log('Error connecting to database:', err);
    return;
  }

new sql.Request().query('SELECT * FROM Users', (err, result) => {
    if (err) {
      console.log('Error querying database:', err);
      return;
    }

    console.log(result.recordset);
    sql.close();
  });
});

I receive the following error output:

Error connecting to database: ConnectionError: Login failed for user ''.
    at C:\Temp\SQL\node_modules\mssql\lib\tedious\connection-pool.js:70:17
    at Connection.onConnect (C:\Temp\SQL\node_modules\tedious\lib\connection.js:1012:9)
    at Object.onceWrapper (node:events:628:26)
    at Connection.emit (node:events:513:28)
    at Connection.emit (C:\Temp\SQL\node_modules\tedious\lib\connection.js:1040:18)
    at C:\Temp\SQL\node_modules\tedious``lib\connection.js:2519:18
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ELOGIN',
  originalError: ConnectionError: Login failed for user ''.
      at Login7TokenHandler.onErrorMessage (C:\Temp\SQL\node_modules\tedious\lib\token\handler.js:245:19)
      at Readable.<anonymous> (C:\Temp\SQL\node_modules\tedious\lib\token\token-stream-parser.js:26:33)
      at Readable.emit (node:events:513:28)
      at addChunk (node:internal/streams/readable:324:12)
      at readableAddChunk (node:internal/streams/readable:297:9)
      at Readable.push (node:internal/streams/readable:234:10)
      at next (node:internal/streams/from:98:31)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    code: 'ELOGIN',
    isTransient: undefined
  }
}

I have attempted to manually enter a username as an option in the config block but nothing changes, it shouldn't be a requirement anyway due to the authentication method being Windows Authentication which is covered by the Trusted Connection option.

If I swap the above code to use msnodesqlv8 as the driver and install Microsoft SQL Server 2012 Native Client driver, the code executes with success but this is not a viable option to require a manual install of a driver.

0

There are 0 answers