Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Failed to connect to PC-Name\SQLEXPRESS – Node.js

I am trying to connect to my SQL Server database via node.js (Express), but the connection cannot be established. The connection is successfully connected via SQL Server Management Studio, but through my code, it is not.

Please check my connection code:

const mssql = require('mssql');

var serviceName = 'DESKTOP-877085U\\SQLEXPRESS'
console.log(serviceName);
class DBConnection {
async getConnection() {
   try {
     return await mssql.connect({
            user: 'mj',
            password: 'mj',
            server: serviceName,
            database: 'DemoDb',
            port: 1433,
            trustServerCertificate: true,
     });
  }
  catch(error) {
    console.log(error);
  }
}
}
module.exports = new DBConnection();

Once I run the application via CMD, it took less than one minute, and this error appeared:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ConnectionError: Failed to connect to DESKTOP-877085U\SQLEXPRESS in 15000ms

at C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\node_modules\mssql\lib\tedious\connection-pool.js:85:17
at Connection.onConnect (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\node_modules\tedious\lib\connection.js:844:9)
at Object.onceWrapper (node:events:628:26)
at Connection.emit (node:events:513:28)
at Connection.emit (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\node_modules\tedious\lib\connection.js:965:18)
at Connection.connectTimeout (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\node_modules\tedious\lib\connection.js:1194:10)
at Timeout._onTimeout (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\node_modules\tedious\lib\connection.js:1139:12)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
code: ‘ETIMEOUT’,
originalError: ConnectionError: Failed to connect to DESKTOP-877085U\SQLEXPRESS in 15000ms
at Connection.connectTimeout (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\node_modules\tedious\lib\connection.js:1194:26)
at Timeout._onTimeout (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\node_modules\tedious\lib\connection.js:1139:12)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
code: ‘ETIMEOUT’,
isTransient: undefined
}

TypeError: Cannot read properties of undefined (reading ‘request’)

at ProductMSSql.getAllProducts (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\modules\Product\product.mssql.js:5:28)
at async getAllProducts (C:\Users\mo7am\Downloads\Projects\nodejs-sqlserver2\modules\Product\product.js:5:25)

I appreciate your support.

>Solution :

try options like:

".\SQLEXPRESS"
or
"localhost"

Or try to find out what is the host name like below:

SELECT @@SERVERNAME
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading