vensali
asked on
I'm trying to connect to SQL server using nodejs, but I'm getting an error.
I have 3 files:
- server.js
- dboperation.js
- 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;
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
}
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)
})
I'm getting an error stating
Can someone help me figure out the reason why I'm getting this?
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)
(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
https://github.com/tediousjs/tedious/issues/914#issuecomment-612463438
But MS shows
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
minVersion: 'TLSv1'
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();
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.