2017-04-15 91 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); 
      }); 
    }; 
相關問題