TypeError:sqlDb.Connection不是使用Node.js的Rest Service中的构造函数

Sam*_*ath 11 javascript sql-server rest node.js

我正在使用Node.js构建一个简单的REST服务

当我发送GET请求时,我收到一条错误消息:

TypeError:sqlDb.Connection不是Object.exports.executeSql的构造函数

这是我的代码.

settings.js

exports.dbConfig = {
  user: "sa",
  password: "sam",
  server: "localhost\\1433",
  database: "SampleDb",
  port: 1433
};
exports.webPort = 9000;
Run Code Online (Sandbox Code Playgroud)

db.js

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

exports.executeSql = function(sql, callback) {

  var conn = new sqlDb.Connection(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);
  });
};
Run Code Online (Sandbox Code Playgroud)

employee.js

var db = require("../core/db");
exports.getList = function(req, resp) {
  db.executeSql("SELECT * FROM emp", function(data, err) {
    if (err) {
      resp.writeHead(500, "Internal Error Occoured", {
        "Content-type": "text/html"
      });
      resp.write("<html><head><title>500</title></head><body>500: Internal Error, Details:" + err + "</body></html>");
      resp.end();
    } else {
      resp.writeHead(200, {
        "Content-Type": "application/json"
      });
      resp.write(JSON.stringify(data));
    }
    resp.end();
  });
};
Run Code Online (Sandbox Code Playgroud)

gok*_*and 13

更改ConnectionConnectionPool

 const conn = new sqlDb.ConnectionPool(settings.dbConfig);
 conn.connect();
Run Code Online (Sandbox Code Playgroud)

从mssql包的npm文档:

3.x到4.x的更改 - Connection已重命名为ConnectionPool.

https://www.npmjs.com/package/mssql#3x-to-4x-changes