question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sample code failing (pool is not defined / pool.on is not a function)

See original GitHub issue

disclaimer: 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:

  1. 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)

. .

  1. 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:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
dhensbycommented, Mar 28, 2019

This would bring pool into scope

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
})

@Palloquin as an experienced programmer I’m sure you understand the concept of local variables. In JS variables are inherited through scopes


let a = 'foo';

function fooFunction() {
    let b = 'bar';
    console.log(a); // logs 'foo'
    console.log(b); // logs 'bar'
}

function barFunction() {
    console.log(a); // logs 'foo'
    console.log(b); // logs undefined
}

You’re effectively trying to access b in a function where it is not accessible.

0reactions
dhensbycommented, Mar 29, 2019

This is a valid bug with the docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

pool.query is not a function - Using npm mysql package to use ...
If you use module.exports=pool in your dbHandler then your code will work as is. If not just use my answer : var pool...
Read more >
7 Multiprocessing Pool Common Errors in Python
In this tutorial you will discover the common errors when using multiprocessing pools in Python and how to fix each in turn.
Read more >
Node.js with MySQL - w3resource
It is written in JavaScript, does not require compiling. ... A connection pool is a cache of database connections maintained so that the ......
Read more >
node-mssql | Microsoft SQL Server client for Node.js
Also notice that we do not close the global pool by calling sql.close() ... The following code is an example of a custom...
Read more >
Why your multiprocessing Pool is stuck (it's full of sharks!)
You check CPU usage—nothing happening, it's not doing any work. What's going on? In many cases you can fix this with a single...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found