2017-04-15 28 views
0

我使用纯的node.js我已经创建了一个8080端口侦听的GET请求的HTML服务器有REST的应用程序。如果请求端点/雇员,例如(http://localhost:8080/employees)时,反应应该是雇员表的一个记录。当我做了(http://localhost:8080/employees GET请求我收到发现下面的错误是db.js:类型错误:SQLDB是不是构造如何修复类型错误: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] } 
+0

做'的console.log(SQLDB)'后要求发言,并检查它暴露 –

+0

我加入了日志我的问题的结束。所需的语句应该从server.js进行访问,但我不知道如何引用所以我增加了它db.js.如果它会帮助你可以引用https://www.npmjs.com/package/mssql#3x-to-4x-changes看看回调 –

+0

文件似乎在说“总是将错误监听器创建的连接。不管什么时候去错了,会发出一个错误,如果没有听众它将与未被捕获的错误崩溃的应用程序的连接“。 – pvg

回答

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); 
      }); 
    }; 
相关问题