使用nodejs连接数据库时出错

时间:2018-02-05 09:50:47

标签: node.js sql-server-2012

app.get('/', function (req, res) {
    var config = {
        //user: 'sa',
       // password: '',
        server: 'localhost',
        database: 'TestDB',
        debug: true
    };

    // connect to your database
    sql.connect(config, function (err) {

        if (err) console.log(err);

        // create Request object
        var request = new sql.Request();

        // query to the database and get the records
        request.query('select * from userInfo', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

            //console.log(recordset);
            sql.close();
        });
    });

});

我希望在没有用户和密码的情况下进行连接,并在通过命令提示符执行时出现此错误,并且有一些可信的连接查询我无法找到请帮助

 Error: Login failed for user ''.
    at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:293:19)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:191:7)
    at Connection.processLogin7Response (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1148:16)
    at Connection.message (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1660:14)
    at Connection.dispatchEvent (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:863:38)
    at MessageIO.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:757:18)
    at emitNone (events.js:86:13)
    at MessageIO.emit (events.js:188:7)
  code: 'ELOGIN',
  originalError:
   { ConnectionError: Login failed for user ''.
       at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
       at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:507:33)
       at emitOne (events.js:96:13)
       at Parser.emit (events.js:191:7)
       at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
       at emitOne (events.js:96:13)
       at Parser.emit (events.js:191:7)
       at addChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:284:12)
       at readableAddChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:271:11)
       at Parser.Readable.push (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:238:10) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
  name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
    at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
    at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
    at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
    at D:\Nodejs\UsersCreate\app.js:119:17
    at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7)
    at process._tickCallback (internal/process/next_tick.js:109:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
GET / 200 36.940 ms - -

请帮我解决此问题,并希望使用窗口身份验证连接此数据库

2 个答案:

答案 0 :(得分:0)

您可以尝试:

 options: {
        trustedConnection: true
    }

答案 1 :(得分:0)

首先安装NPM Package,

npm install msnodesqlv8

然后将配置变量更改为

const sql = require('mssql/msnodesqlv8')
var config = {
    server: 'localhost',
    driver: 'msnodesqlv8',
    database: 'TestDB',
    debug: true,
    options: {
        trustedConnection: true
    }
};