Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: sqlDb.Connection is not a constructor in Rest Service using Node.js

I'm building a simple REST Service with Node.js

When I send a GET request I'm getting an error saying:

TypeError: sqlDb.Connection is not a constructor at Object.exports.executeSql

Here is my code.

settings.js

exports.dbConfig = {
  user: "sa",
  password: "sam",
  server: "localhost\\1433",
  database: "SampleDb",
  port: 1433
};
exports.webPort = 9000;

db.js

var sqlDb = require("mssql");
var settings = require("../settings");

exports.executeSql = function(sql, callback) {

  var conn = new sqlDb.Connection(settings.dbConfig);
  conn.connect()
  .then(function() {
    var req = new sqlDb.Request(conn);
    req.query(sql)
    .then(function(recordset) {
      callback(recordset);
    })
    .catch(function(err) {
      console.log(err);
      callback(null, err);
    });
  })
  .catch(function(err) {
    console.log(err);
    callback(null, err);
  });
};

employee.js

var db = require("../core/db");
exports.getList = function(req, resp) {
  db.executeSql("SELECT * FROM emp", function(data, err) {
    if (err) {
      resp.writeHead(500, "Internal Error Occoured", {
        "Content-type": "text/html"
      });
      resp.write("<html><head><title>500</title></head><body>500: Internal Error, Details:" + err + "</body></html>");
      resp.end();
    } else {
      resp.writeHead(200, {
        "Content-Type": "application/json"
      });
      resp.write(JSON.stringify(data));
    }
    resp.end();
  });
};
like image 928
Sameera Sampath Avatar asked Dec 30 '17 10:12

Sameera Sampath


1 Answers

Change Connection to ConnectionPool

 const conn = new sqlDb.ConnectionPool(settings.dbConfig);
 conn.connect();

From the mssql package's npm documentation:

3.x to 4.x changes - Connection was renamed to ConnectionPool.

https://www.npmjs.com/package/mssql#3x-to-4x-changes

like image 192
gokcand Avatar answered Sep 20 '22 01:09

gokcand