amin zare
amin zare

Reputation: 23

sql.Connection is not a constructor

I am trying to establish a basic connection to SQL Server, but I am getting the following error:

TypeError: sql.Connection is not a constructor

My code in server.js

var express = require('express');
var bodyparser = require('body-parser');
//1.
var sql = require('mssql');
//2.
var app=express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
var config = {
    server: 'localhost',
    database: 'HouseCost',
    user: 'sa',
    password: '5061369',
    port: 1433
};
//app.use("/GetAllSells",function(req,resp,next){
function loadAction() {
    //4.
    var dbConn = new sql.Connection(config);
    //5.
    dbConn.connect().then(function () {
        //6.
        var request = new sql.Request(dbConn);
        //7.
        request.query("select * from Sells").then(function (recordSet) {
            console.log(recordSet);
            dbConn.close();
        }).catch(function (err) {
            //8.
            console.log(err);
            dbConn.close();
        });
    }).catch(function (err) {
        //9.
        console.log(err);
    });
}
//10.
loadAction();

 app.listen(8000);
 console.log("Server Is Run");

and my package.json is:

{
 "name": "amin",
 "version": "1.0.0",
 "description": "",
 "main": "ServerSide.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": {},

 "author": "",
 "license": "ISC"
}

Upvotes: 2

Views: 4429

Answers (1)

Ed Bangga
Ed Bangga

Reputation: 13026

your nodeJs module mssql -> sql.Connection() is not a constructor. You should be using .ConnectionPool(), or if you want asynchronous call you can use .connect()

   var dbConn = new sql.ConnectionPool(config);

Upvotes: 3

Related Questions