node连接mssql出错请指教
发布于 1 年前 作者 without-yy 1244 次浏览 来自 问答
const dbConfig = require('./config').dbConfig;
const sql = require('mssql');

sql.connect(dbConfig).then(function() {

    new sql.Request().query(`select * from T_Weibo_Uids`).then(function(recordset) {
        console.dir(recordset);
    }).catch(function(err) {
        // ... query error checks
        console.log('query出错');
        throw err;
    });
    
}).catch(function(err) {
    console.log('连接出错');
    // ... connect error checks
    throw err;
});

配置如下
dbConfig :{
        user: 'xxx',
        password: 'xxxx',
        server: 'xxxx',
        database: 'xxxx',
        pool: {
            max: 10,
            min: 0,
            idleTimeoutMillis: 30000
        }
    }

连接出错
(node:14762) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: "listener" argument must be a function
(node:14762) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


//这样写还是同样的错误,是不是哪里少了个参数,完全看不懂这个错误
sql.connect(dbConfig, function(err) {
    if(err) throw err;
    console.log('连接成功');
});
events.js:307
    throw new TypeError('"listener" argument must be a function');
    ^

TypeError: "listener" argument must be a function
    at Socket.once (events.js:307:11)
    at Socket.connect (net.js:943:10)
    at Connection.connectOnPort (/Users/xx/Desktop/work/weiboApi/node_modules/tedious/lib/connection.js:429:19)
    at Connection.connect (/Users/xx/Desktop/work/weiboApi/node_modules/tedious/lib/connection.js:402:21)
    at Connection.initialiseConnection (/Users/xx/Desktop/work/weiboApi/node_modules/tedious/lib/connection.js:77:12)
    at Connection.enter (/Users/xx/Desktop/work/weiboApi/node_modules/tedious/lib/connection.js:975:19)
    at Connection.transitionTo (/Users/xx/Desktop/work/weiboApi/node_modules/tedious/lib/connection.js:508:33)
    at new Connection (/Users/xx/Desktop/work/weiboApi/node_modules/tedious/lib/connection.js:66:10)
    at Object.cfg_pool.create (/Users/xx/Desktop/work/weiboApi/node_modules/mssql/lib/tedious.js:375:19)
    at Pool._createResource (/Users/xx/Desktop/work/weiboApi/node_modules/generic-pool/lib/generic-pool.js:354:17)
3 回复

肯定是API弄错了,错误说的很明白…TypeError: "listener" argument must be a function

@DevinXian const sql = require(‘mssql’);

这是官方的例子 还会报同样的错误
sql.connect("mssql://username:password@localhost/database").then(() => {
	return sql.query`select * from mytable where id = ${value}`;
}).then(result => {
	console.dir(result);
}).catch(err => {
	// ... connect error checks
})

已经解决,安装了这个版本可以使用 "mssql": “^4.0.0-alpha2”,

回到顶部