4

I am very beginner with node.js and javscript itself. I am trying to establish a basic connection to SQLServer but I am getting the following error:

TypeError: sql.Connection is not a function

I am following the recommendations of the mssql package git repository

//get an instance of sqlserver 
var sql = require('mssql');

//set up a sql server credentials
var config = {
server: 'localhost',
database: 'trafiyadb',
user: 'jhonycage',
password:  'juan1014',
port: 1433
};


function con() {
var dbConn = new sql.Connection(config);
dbConn.connect().then(function(){
    console.log("connected")
}).catch(function (err) {
    console.log(err);
})
}

con();

and this is the package.json

{
 "name": "trafiyaapi",
 "version": "1.0.0",
 "description": "",
 "main": "server.js\u001b[A\u001b[B\u001b[B\u001b[B\u001b[A\u001b[B",
 "dependencies": {
 "express": "^4.15.2",
 "body-parser": "^1.17.1",
 "mssql": "^4.0.4",
 "jsonwebtoken": "^7.4.0"
 },
 "devDependencies": {},
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
 "start": "node server.js"
 },
 "author": "",
 "license": "ISC"
 }

I am just executing

$node server

I must be doing something wrong, how can i connect to database?

1 Answer 1

5

In fact, the function Connection doesn't exist. According to the documentation, to connect to a database, you can use the class ConnectionPool, or the method connect.

var dbConn = new sql.ConnectionPool(config);

dbConn.connect(function(err) {
    // ...
});

// OR ...

sql.connect(config).then(function(dbConn) {
   // ...
})
Sign up to request clarification or add additional context in comments.

3 Comments

It worked indeed, thanks a bunch but now I am getting following error: { [ConnectionError: Failed to connect to localhost:1433 - Could not connect (seq uence)] code: 'ESOCKET', originalError: { [ConnectionError: Failed to connect to localhost:1433 - Could not connect ( sequence)] message: 'Failed to connect to localhost:1433 - Could not connect (sequence )', code: 'ESOCKET' }, name: 'ConnectionError' }
Now you have to make sure that your database process is running. double check the port and try to change localhost to 127.0.0.1.
just changed port number and server to 127.0.0.1 and is connected, I have to thank you sir for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.