Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NPM MSSQL-错误:uncaughtException:无法读取属性';发布';空的_Javascript_Sql Server_Node.js_Npm - Fatal编程技术网

Javascript NPM MSSQL-错误:uncaughtException:无法读取属性';发布';空的

Javascript NPM MSSQL-错误:uncaughtException:无法读取属性';发布';空的,javascript,sql-server,node.js,npm,Javascript,Sql Server,Node.js,Npm,我正在尝试使用模块连接到mssql服务器。我得到下面提到的错误,我试图搜索它,但找不到任何有帮助的除了少数用户已经报告它没有任何成功 error: uncaughtException: Cannot read property 'release' of null date=Sat Jul 15 2017 02:03:59 GMT+0000 (UTC), pid=10150, uid=1000, gid=1000, cwd=/home/ubuntu/server/gcap-server-exp,

我正在尝试使用模块连接到mssql服务器。我得到下面提到的错误,我试图搜索它,但找不到任何有帮助的除了少数用户已经报告它没有任何成功

error: uncaughtException: Cannot read property 'release' of null date=Sat Jul 15 2017 02:03:59 GMT+0000 (UTC), pid=10150, uid=1000, gid=1000, cwd=/home/ubuntu/server/gcap-server-exp, execPath=/usr/bin/nodejs, version=v6.11.1, argv=[/usr/bin/nodejs, /usr/lib/node_modules/pm2/lib/ProcessContainerFork.js], rss=70537216, heapTotal=47235072, heapUsed=35834656, external=18214141, loadavg=[0.14794921875, 0.10498046875, 0.02880859375], uptime=2206463
TypeError: Cannot read property 'release' of null
    at ConnectionPool.release (/home/ubuntu/server/gcap-server-exp/node_modules/atpl-models/node_modules/mssql/lib/base.js:199:14)
    at Request.tds.Request.err [as userCallback] (/home/ubuntu/server/gcap-server-exp/node_modules/atpl-models/node_modules/mssql/lib/tedious.js:892:25)
    at Request._this.callback (/home/ubuntu/server/gcap-server-exp/node_modules/atpl-models/node_modules/tedious/lib/request.js:47:27)
    at Connection.message (/home/ubuntu/server/gcap-server-exp/node_modules/atpl-models/node_modules/tedious/lib/connection.js:1401:27)
    at Connection.dispatchEvent (/home/ubuntu/server/gcap-server-exp/node_modules/atpl-models/node_modules/tedious/lib/connection.js:687:45)
    at MessageIO.<anonymous> (/home/ubuntu/server/gcap-server-exp/node_modules/atpl-models/node_modules/tedious/lib/connection.js:602:18)
    at emitNone (events.js:86:13)
    at MessageIO.emit (events.js:185:7)
    at ReadablePacketStream.<anonymous> (/home/ubuntu/server/gcap-server-exp/node_modules/atpl-models/node_modules/tedious/lib/message-io.js:102:16)
    at emitOne (events.js:96:13)
上面的
sqlConfig
参数为-

sqlConfig = {
  user: 'username',
  password: '******',
  server: 'xxx.xxx.xxx.xxx',
  database: 'database',
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000
  }
}

我也遇到了同样的问题,下面是我必须做的来避免这个错误

下面是我的函数,它可以连接到数据库:

async function connectToDb() {
    const pool = await new sql.ConnectionPool(connectionConfig).connect();
    return pool;
}
下面是执行查询的函数:

async function someDatabaseFunction(args, done) {
    let pool;
    let ps;

    try {
        pool = await connectToDb();
        ps = new sql.PreparedStatement(pool);
        ps.input('input1', sql.VarChar(10));

        const sqlStr = 'my query goes here';

        await ps.prepare(sqlStr);
        const result = await ps.execute({ groupId: args.groupId, status: args.status });

        done(null, result);
    } catch (error) {
        done(error);
    } finally {
        if (ps) await ps.unprepare();
        if (pool) pool.close();
    }
}
我遇到的问题是,在finally块中,我调用了
ps.unprepare()
,但没有
等待该函数。我要直接进入
pool.close()
函数
wait
ing
ps.unprepare
为我消除了错误


我不确定它是否适用于您的情况,但它可以,因为您在
sql.connect
之前就调用了
sql.close()
。可能是您正在调用
sql。在空值上关闭
,这就是产生的错误。

看起来在您释放mssql连接时会出现此错误…是的,这是正确的错误是在释放连接时,但是,当您使用mssql模块时,没有手动代码来释放连接,它自己也会释放连接。顺便说一句,您在代码中释放连接的位置..没有。我不会释放代码中的任何连接,正如你在问题中看到的那样。那么这句话在做什么
sql.close()
是的,应该是这样的,我最近发现了,谢谢你的回答。
async function someDatabaseFunction(args, done) {
    let pool;
    let ps;

    try {
        pool = await connectToDb();
        ps = new sql.PreparedStatement(pool);
        ps.input('input1', sql.VarChar(10));

        const sqlStr = 'my query goes here';

        await ps.prepare(sqlStr);
        const result = await ps.execute({ groupId: args.groupId, status: args.status });

        done(null, result);
    } catch (error) {
        done(error);
    } finally {
        if (ps) await ps.unprepare();
        if (pool) pool.close();
    }
}