Link to home
Start Free TrialLog in
Avatar of vensali
vensaliFlag for India

asked on

I'm trying to connect to SQL server using nodejs, but I'm getting an error.

I have 3 files:

  1. server.js
  2. dboperation.js
  3. dbconfig.js

In the dbconfig.js file I have:

const config = {
    user: 'name',
    password: 'password',
    server: 'localhost',
    database: 'dbname',
    options: {
        trustedServerCertificate: false
    }
}
module.exports = config;

Open in new window

In the dboperation.js file I have:

const config = require('./dbConfig'),
sql = require('mssql')

const getUsers = async() => {
    try{
        let pool = await sql.connect(config)
        let result1 = await pool.request()
                    .query("Select * from Master_User")
        console.log(result1)
        return employees
    }
    catch(err) {
        console.log(err)
    }
}
module.exports = {
    getUsers
}

Open in new window

In the server.js file I have:

const express = require('express')
const cors = require('cors')
const dbOperation = require('./dbFiles/dbOperation')

dbOperation.getUsers().then(res => {
    console.log(res)
})

Open in new window

I'm getting an error stating

User generated imageCan someone help me figure out the reason why I'm getting this?

Avatar of David Favor
David Favor
Flag of United States of America image

This looks to be a TLS protocol mismatch.

A common debug approach is to connect to the MSSQL instance with the related command line tool (mysql for MariaDB/MySQL, unsure about MSSQL).

If the command line tool connects, then NodeJS is the problem.

If the command line tool fails to connect, then likely you'll have to create the proper GRANT of firewall rules (if required) for connection to work.

As with all debugging you work incrementally to determine where the real problem exists.
Avatar of vensali

ASKER

I am able to connect to MYSQL  from the same file if i change config parameters to mysql.

 (This looks to be a TLS protocol mismatch. -- does this mean i need to do any  windows registry changes)
I don't use node myself and saw there are no other answers so far. I found what may be an option with using 
 minVersion: 'TLSv1'

Open in new window


https://github.com/tediousjs/tedious/issues/914#issuecomment-612463438

But MS shows
    var Connection = require('tedious').Connection;  
    var config = {  
        server: 'your_server.database.windows.net',  //update me
        authentication: {
            type: 'default',
            options: {
                userName: 'your_username', //update me
                password: 'your_password'  //update me
            }
        },
        options: {
            // If you are on Microsoft Azure, you need encryption:
            encrypt: true,
            database: 'your_database'  //update me
        }
    };  
    var connection = new Connection(config);  
    connection.on('connect', function(err) {  
        // If no error, then good to proceed.
        console.log("Connected");  
    });
    
    connection.connect();

Open in new window



https://docs.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js?view=sql-server-ver15


ASKER CERTIFIED SOLUTION
Avatar of vensali
vensali
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial