Saturday 15 April 2017

How to fix TypeError: sqlDb is not a constructor

I have REST app using pure node.js I have created an HTML server that listens on port 8080 for a GET request. IF the request endpoint is /employees, for example (http://localhost:8080/employees), the response should be a recordset of the employee table. When I make the GET request for (http://localhost:8080/employees I am receiving the following error found is db.js: TypeError: sqlDb is not a constructor

I am stuck at this point and any help would be greatly appreciated.

Here is the js code:

db.js

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

    exports.executeSql = function (sql, callback) {
        var conn = new sqlDb.ConnectionPool(settings.dbConfig);
        conn.connect()
            .then(function () {
                var req = new sqlDb(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);
            });
    };

server.js

    const sqlDb = require('mssql/msnodesqlv8')
    var http = require("http");
    var emp = require("../controllers/employees");
    const settings = require("../settings");

    http.createServer(function (req, resp) {
        switch (req.method) {
            case "GET":
                if (req.url === "/") {
                    resp.end();
                }
                else if (req.url === "/employees") {
                    emp.getList(req, resp);
                }
                break;
            case "POST":
                break;
            case "PUT":
                break;
            case "DELETE":
                break;
            default:
                break;
        }

    }).listen(settings.webPort, function () {
        console.log("Server Started Listening at: " + settings.webPort);
    });

employees.js

var db = require("../core/db");

exports.getList = function (req, resp) {
    db.executeSql("SELECT * FROM EMPLOYEE", function (data, err) {
        if (err) {
            resp.writeHead(500, "Internal Error Occured", { "Content-Type": "text/html" });
            resp.write("<html><head><title>500</title></head><body500: Internal Error. Details: " + err + "></body></html>");
        }
        else {
            resp.writeHead(200, {"Content-Type": "application/json"});
            resp.write(JSON.stringify(data));
        }
        resp.end();
    });
};

exports.get = function (req, resp, empno) {

};

exports.add = function (req, resp, reqBody) {

};

exports.update = function (req, resp, reqBody) {

};

exports.delete = function (req, resp, reqBody) {

};

settings.js

exports.dbConfig = {
    user: 'sa',
    password: '123abc',
    server: 'localhost',
    database: 'LWWEBAPP',
    port: 1433
};

exports.webPort = 8080;



via joey.coyle

No comments:

Post a Comment