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();
});
};
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 toConnectionPool
.
https://www.npmjs.com/package/mssql#3x-to-4x-changes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With