Saturday, 6 May 2017

TypeError: sql.Connection is not a function Node.js

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?



via Juanca

No comments:

Post a Comment