I am trying to implement some queries in my server code, but am facing this error:
setImmediate(callback, new ConnectionError('Cannot close a pool while it is connecting'))
ConnectionError: Cannot close a pool while it is connecting
I checked online and it said to use mssql.query instead of a mssql.timeout but my code has both so it got me quite confused. I tried to increase the set timeout from 500 to 1500 thinking the error was because of that, but it still had crashes.
An odd thing is that the server doesn’t always crash. It just happens randomly at times when a query is being executed.
The server essentially sends data for beacons and the floor they are located on.
I’ve been trying to figure this out for quite sometime now. I would appreciate any help.
Here is the parts where the error is happening:
// Handle POST requests for downloading files for beacons
app.post('/beacon', function (req, res) {
// POST variables
var auth = req.body.auth;
var gid = req.body.gid;
var fno = req.body.fno;
// Database Information
const config = {
user: 'RANDOM',
password: auth,
server: 'localhost',
database: 'beacons',
"options": {
"encrypt": true,
"enableArithAbort": true
},
port: 1499
};
try{
// Connect to database
mssql.connect(config, function (err) {
// Create request handler and query for files
var request = new mssql.Request();
var query = `SELECT beacon_file FROM dbo.beacons WHERE group_id = ${gid} AND floor_num = ${fno}`;
// Query for file to download
request.query(query,
function (err, records) {
try{
if (err){
console.log(err);
throw err;
}
try {
// If there are files associated with the given group ID
if (records.rowsAffected != '0'){
// Get filename & send for download
// NOTE: We can either send a download or send text, not both using the current setup
var fileName = 'beaconfiles/' + records.recordset[0].beacon_file;
res.download(fileName, function (derr) {
if (derr) {
throw(derr);
}
else {
console.log(`Successfully sent ${fileName} for download.`)
}
});
}
else{
res.send(`<p>There are no files with that group ID.</p>
<a href="javascript:history.back()">Go Back</a>`);
}
}
catch (err) {
console.log(`There was an error: ${err} `);
res.send(`<p>There was an error. Make sure the group ID is correct: ${err}</p>
<a href="javascript:history.back()">Go Back</a>` );
}
}
catch(err){
res.send(`<p>There was an error getting the file: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});
setTimeout(function(){
mssql.close();
}, 1500);
});
}
catch(err){
console.log(`There was an issue connecting to the database, check the authorization: ${err} `);
res.send(`<p>There was an issue connecting to the database, check the authorization: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});
// Handle POST requests for downloading files for beacons
app.post('/floor', function (req, res) {
// POST variables
var auth = req.body.auth;
var gid = req.body.gid;
var fno = req.body.fno;
// Database Information
const config = {
user: 'RANDOM',
password: auth,
server: 'localhost',
database: 'beacons',
"options": {
"encrypt": true,
"enableArithAbort": true
},
port: 1499
};
try{
// Connect to database
mssql.connect(config, function (err) {
// Create request handler and query for files
var request = new mssql.Request();
var query = `SELECT floor_file FROM dbo.beacons WHERE group_id = ${gid} AND floor_num = ${fno}`;
// Query for file to download
request.query(query,
function (err, records) {
try{
if (err){
console.log(err);
throw err;
}
try {
// If there are files associated with the given group ID
if (records.rowsAffected != '0'){
// Get filename & send for download
// NOTE: We can either send a download or send text, not both using the current setup
var fileName = 'beaconfiles/' + records.recordset[0].floor_file;
res.download(fileName, function (derr) {
if (derr) {
throw(derr);
}
else {
console.log(`Successfully sent ${fileName} for download.`)
}
});
}
else{
res.send(`<p>There are no files with that group ID.</p>
<a href="javascript:history.back()">Go Back</a>`);
}
}
catch (err) {
console.log(`There was an error: ${err} `);
res.send(`<p>There was an error. Make sure the group ID is correct: ${err}</p>
<a href="javascript:history.back()">Go Back</a>` );
}
}
catch(err){
res.send(`<p>There was an error getting the file: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});
setTimeout(function(){
mssql.close();
}, 1000);
});
}
catch(err){
console.log(`There was an issue connecting to the database, check the authorization: ${err} `);
res.send(`<p>There was an issue connecting to the database, check the authorization: ${err}</p>
<a href="javascript:history.back()">Go Back</a>`);
}
});
