简体   繁体   中英

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;

This is the console.log(sqlDb);

{ ConnectionPool: [Function: ConnectionPool],
  Transaction: [Function: Transaction],
  Request: [Function: Request],
  PreparedStatement: [Function: PreparedStatement],
  ConnectionError: [Function: ConnectionError],
  TransactionError: [Function: TransactionError],
  RequestError: [Function: RequestError],
  PreparedStatementError: [Function: PreparedStatementError],
  Table:
   { [Function: Table]
     fromRecordset: [Function: fromRecordset],
     parseName: [Function: parseName] },
  ISOLATION_LEVEL:
   { READ_UNCOMMITTED: 1,
     READ_COMMITTED: 2,
     REPEATABLE_READ: 3,
     SERIALIZABLE: 4,
     SNAPSHOT: 5 },
  TYPES:
   { VarChar: [sql.VarChar],
     NVarChar: [sql.NVarChar],
     Text: [sql.Text],
     Int: [sql.Int],
     BigInt: [sql.BigInt],
     TinyInt: [sql.TinyInt],
     SmallInt: [sql.SmallInt],
     Bit: [sql.Bit],
     Float: [sql.Float],
     Numeric: [sql.Numeric],
     Decimal: [sql.Decimal],
     Real: [sql.Real],
     Date: [sql.Date],
     DateTime: [sql.DateTime],
     DateTime2: [sql.DateTime2],
     DateTimeOffset: [sql.DateTimeOffset],
     SmallDateTime: [sql.SmallDateTime],
     Time: [sql.Time],
     UniqueIdentifier: [sql.UniqueIdentifier],
     SmallMoney: [sql.SmallMoney],
     Money: [sql.Money],
     Binary: [sql.Binary],
     VarBinary: [sql.VarBinary],
     Image: [sql.Image],
     Xml: [sql.Xml],
     Char: [sql.Char],
     NChar: [sql.NChar],
     NText: [sql.NText],
     TVP: [sql.TVP],
     UDT: [sql.UDT],
     Geography: [sql.Geography],
     Geometry: [sql.Geometry],
     Variant: [sql.Variant] },
  MAX: 65535,
  map:
   [ { js: [Function: String], sql: [sql.NVarChar] },
     { js: [Function: Number], sql: [sql.Int] },
     { js: [Function: Boolean], sql: [sql.Bit] },
     { js: [Function: Date], sql: [sql.DateTime] },
     { js: [Object], sql: [sql.VarBinary] },
     { js: [Object], sql: [sql.TVP] },
     register: [Function] ],
  VarChar: [sql.VarChar],
  VARCHAR: [sql.VarChar],
  NVarChar: [sql.NVarChar],
  NVARCHAR: [sql.NVarChar],
  Text: [sql.Text],
  TEXT: [sql.Text],
  Int: [sql.Int],
  INT: [sql.Int],
  BigInt: [sql.BigInt],
  BIGINT: [sql.BigInt],
  TinyInt: [sql.TinyInt],
  TINYINT: [sql.TinyInt],
  SmallInt: [sql.SmallInt],
  SMALLINT: [sql.SmallInt],
  Bit: [sql.Bit],
  BIT: [sql.Bit],
  Float: [sql.Float],
  FLOAT: [sql.Float],
  Numeric: [sql.Numeric],
  NUMERIC: [sql.Numeric],
  Decimal: [sql.Decimal],
  DECIMAL: [sql.Decimal],
  Real: [sql.Real],
  REAL: [sql.Real],
  Date: [sql.Date],
  DATE: [sql.Date],
  DateTime: [sql.DateTime],
  DATETIME: [sql.DateTime],
  DateTime2: [sql.DateTime2],
  DATETIME2: [sql.DateTime2],
  DateTimeOffset: [sql.DateTimeOffset],
  DATETIMEOFFSET: [sql.DateTimeOffset],
  SmallDateTime: [sql.SmallDateTime],
  SMALLDATETIME: [sql.SmallDateTime],
  Time: [sql.Time],
  TIME: [sql.Time],
  UniqueIdentifier: [sql.UniqueIdentifier],
  UNIQUEIDENTIFIER: [sql.UniqueIdentifier],
  SmallMoney: [sql.SmallMoney],
  SMALLMONEY: [sql.SmallMoney],
  Money: [sql.Money],
  MONEY: [sql.Money],
  Binary: [sql.Binary],
  BINARY: [sql.Binary],
  VarBinary: [sql.VarBinary],
  VARBINARY: [sql.VarBinary],
  Image: [sql.Image],
  IMAGE: [sql.Image],
  Xml: [sql.Xml],
  XML: [sql.Xml],
  Char: [sql.Char],
  CHAR: [sql.Char],
  NChar: [sql.NChar],
  NCHAR: [sql.NChar],
  NText: [sql.NText],
  NTEXT: [sql.NText],
  TVP: [sql.TVP],
  UDT: [sql.UDT],
  Geography: [sql.Geography],
  GEOGRAPHY: [sql.Geography],
  Geometry: [sql.Geometry],
  GEOMETRY: [sql.Geometry],
  Variant: [sql.Variant],
  VARIANT: [sql.Variant],
  connect: [Function: connect],
  close: [Function: close],
  on: [Function: on],
  off: [Function: removeListener],
  removeListener: [Function: removeListener],
  query: [Function: query],
  batch: [Function: batch],
  Promise: [Getter/Setter] }

I had a mistake in my code that was causing the problem. The line that reads var req = new sqlDb.(conn); should read var req = new sqlDb.Request(conn); This is the complete corrected db.js:

const sqlDb = require('mssql/msnodesqlv8')

var settings = require("../settings");
var server = require("./server");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM