如何修复TypeError:sqlDb不是构造函数

时间:2017-04-15 19:41:38

标签: javascript node.js rest

我有使用纯node.js的REST应用程序我创建了一个HTML服务器,它在端口8080上侦听GET请求。如果请求端点是/ employees,例如(http://localhost:8080/employees),则响应应该是employee表的记录集。当我发出GET请求(http://localhost:8080/employees时,我收到以下错误,发现是db.js:TypeError:sqlDb不是构造函数

我被困在这一点上,任何帮助都将不胜感激。

这是js代码:

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;

这是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] }

1 个答案:

答案 0 :(得分:0)

我的代码中出现了导致问题的错误。读取var req = new sqlDb的行。(conn);应该读取var req = new sqlDb.Request(conn);这是完整更正的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);
            });
    };