sample code failing (pool is not defined / pool.on is not a function)
See original GitHub issuedisclaimer: Though I have a lot of experience on other platforms, I’m very new to node js, also did a lot of javascript but very little using promises…
I’m trying to connect to our SQL server using the standard samples supplied in the documentation. I get all kinds of errors, likely I am doing something wrong or perhapsmy is installation messed up?
Expected behaviour:
I run sample code as provided and expect not to get any errors
Actual behaviour:
- connect to db and do a query, then a stored procedure
const sql = require('mssql')
const config = {
user: 'user',
password: 'pass',
server: 'server',
database: 'master',
}
sql.connect(config).then(pool => {
return pool.request()
.query('select * from sys.tables ')
}).then(result => {
console.dir(result)
return pool.request() /* <-- THIS LINE! */
.input('input_parameter', sql.Int, 10)
.output('output_parameter', sql.VarChar(50))
.execute('procedure_name')
}).then(result => {
console.dir(result)
}).catch(err => {
console.log("error at line24: ", err)
})
sql.on('error', err => {
console.log("error at line28: ", err)
// ... error handler
})
This results in in valid records for the first request and then:
error at line24: ReferenceError: pool is not defined at sql.connect.then.then.result (C:\nodeJs\sql.js:16:5) at process._tickCallback (internal/process/next_tick.js:68:7)
. .
- using connection pools:
const sql = require('mssql')
const config = {
user: 'user',
password: 'pass',
server: 'server',
database: 'master',
}
// promise style:
const pool2 = new sql.ConnectionPool(config, err => {
// ... error checks
}).connect();
pool2.on('error', err => {
// ... error handler
})
Gives this error:
pool2.on(‘error’, err => { ^
TypeError: pool2.on is not a function at Object.<anonymous> (C:\nodeJs\sql.js:15:7) at Module._compile (internal/modules/cjs/loader.js:701:30) at Object.Module._extensions…js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal/modules/cjs/loader.js:531:3) at Function.Module.runMain (internal/modules/cjs/loader.js:754:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
. . I’m not sure if this is valid or related, but when I change the code to:
const sql = require('mssql')
const config = {
user: 'user',
password: 'pass',
server: 'server',
database: 'master',
}
// promise style:
const pool2 = new sql.ConnectionPool(config, err => {
// ... error checks
}) /* <-- MOVED THE CONNECT METHOD FROM HERE */
pool2.connect(); /* <-- TO HERE */
pool2.on('error', err => {
// ... error handler
})
I get this error:
C:\nodeJs>node sql.js (node:2348) UnhandledPromiseRejectionWarning: ConnectionError: Already connecting to database! Call close before connecting to different database. at ConnectionPool._connect (C:\nodeJs\node_modules\mssql\lib\base.js:238:23) at PromiseLibrary (C:\nodeJs\node_modules\mssql\lib\base.js:220:19) at new Promise (<anonymous>) at ConnectionPool.connect (C:\nodeJs\node_modules\mssql\lib\base.js:219:12) at Object.<anonymous> (C:\nodeJs\sql.js:15:7) at Module._compile (internal/modules/cjs/loader.js:701:30) at Object.Module._extensions…js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal/modules/cjs/loader.js:531:3) (node:2348) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:2348) [DEP0018] 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.
Software versions
- NodeJS: v10.15.3 (on windows 10)
- node-mssql: 5.0.4
- SQL Server: Sql Server 2012
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
This would bring
pool
into scope@Palloquin as an experienced programmer I’m sure you understand the concept of local variables. In JS variables are inherited through scopes
You’re effectively trying to access
b
in a function where it is not accessible.This is a valid bug with the docs