ConnectionError: Failed to connect to DESKTOP-G5KP88I:1433 - self signed certificate

I am working on the node js project. and I am trying to connect the SQL server with the node js.

Here is my code for connecting the SQL server.

const sql = require("mssql");

const {dbConfig} = require("./dbConnection") ;

var connection = new sql.ConnectionPool(dbConfig);

connection.connect((err) => {
  console.log(err);
});

and I am passing the below object to the sql.ConnectionPool method

dbConfig = {
    name: "default",
    server: "ServerName",
    host: "localhost",
    database: "dbName",
    user: "dbuser",
    password: "yourdbpassword",
}

and it throws me the below error

ConnectionError: Failed to connect to DESKTOP-G5KP88I:1433 - self signed certificate
    at S:\chatApp\chatApp\chatBackend\node_modules\mssql\lib\tedious\connection-pool.js:71:17
    at Connection.onConnect (S:\chatApp\chatApp\chatBackend\node_modules\tedious\lib\connection.js:1037:9)
    at Object.onceWrapper (events.js:422:26)
    at Connection.emit (events.js:315:20)
    at Connection.emit (S:\chatApp\chatApp\chatBackend\node_modules\tedious\lib\connection.js:1065:18)
    at Connection.socketError (S:\chatApp\chatApp\chatBackend\node_modules\tedious\lib\connection.js:1663:12)
    at Socket.<anonymous> (S:\chatApp\chatApp\chatBackend\node_modules\tedious\lib\connection.js:1427:14)
    at Socket.emit (events.js:327:22)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3) {
  code: 'ESOCKET',
  originalError: ConnectionError: Failed to connect to DESKTOP-G5KP88I:1433 - self signed certificate
      at ConnectionError (S:\chatApp\chatApp\chatBackend\node_modules\tedious\lib\errors.js:13:12)
      at Connection.socketError (S:\chatApp\chatApp\chatBackend\node_modules\tedious\lib\connection.js:1663:56)
      at Socket.<anonymous> (S:\chatApp\chatApp\chatBackend\node_modules\tedious\lib\connection.js:1427:14)
      at Socket.emit (events.js:327:22)
      at emitErrorNT (internal/streams/destroy.js:106:8)
      at emitErrorCloseNT (internal/streams/destroy.js:74:3)
      at processTicksAndRejections (internal/process/task_queues.js:80:21) {
    code: 'ESOCKET'
  }
}

Solution:

The problem was in the configuration object

I added one property in the configuration object and the property name is

trustServerCertificate: true

here is the updated object which I passed in the connection method

dbConfig = {
    name: "default",
    server: "ServerName",
    host: "localhost",
    database: "dbName",
    user: "dbuser",
    password: "yourdbpassword",
    trustServerCertificate: true,
}

Thank you.




Add comment