I created config file for database like below:
module.exports.connection = {
sqlserver: {
user: 'user_name',
password: 'password',
server: 'servername\\instancename',
database: 'database_name',
port: 1433,
},
};
I have a simple table in database:
I'm trying get a data from table above like below:
const sql = require('mssql');
const config = require('../config/connection');
sql.connect(config.connection.sqlserver).then((pool) => pool.request().query('select type from dbo.category')).then((result) => {
console.log(result);
}).catch((err) => {
console.log(err);
});
sql.close();
Unfortunately I doesn't see anything in console and I don't have any error so I can't find what's wrong. I tried also:
sql.connect(config.connection.sqlserver).then((pool) => pool.request().query('select type from dbo.category')).catch((err) => {
console.log(err);
}).then((result) => {
console.log(result);
}).catch((err) => {
console.log(err);
});
sql.close();
I used https://www.npmjs.com/package/mssql#promises. I'm trying to do it on my nodeJs server. Any ideas?

select * from categoryand check what it returns, and also add some string to console log to see if then or catch worked. Like this 'console.log('then() handled. Result: ', result);'sql.connect(config.connection.sqlserver).then( result => console.log('connected!')).catch((err) => console.log('some error'));sql.close()calling before you finish your query. Just place it into lastthen, and also into catch (to close connection if error). To close it after you'll done. Or use async/await instead of then/catch.