3

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:

enter image description here

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?

6
  • try change your query like this 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);' Commented Oct 2, 2019 at 14:50
  • I tried and nothing. Any errors. Commented Oct 2, 2019 at 14:56
  • ok, are you checked that this code was called? Add console before and after this code, and also you can try test only connection first sql.connect(config.connection.sqlserver).then( result => console.log('connected!')).catch((err) => console.log('some error')); Commented Oct 2, 2019 at 15:03
  • 1
    oh, yeah. It happens b/c sql.close() calling before you finish your query. Just place it into last then, and also into catch (to close connection if error). To close it after you'll done. Or use async/await instead of then/catch. Commented Oct 3, 2019 at 8:57
  • 1
    Everything works. Thank you! Commented Oct 3, 2019 at 14:12

1 Answer 1

1

I resolved my problem. Thanks for Julia Shestakova suggestions. My query function looks like this:

const sql = require('mssql');
const config = require('./config/connection');
module.exports = {
  query(query) {
    return sql.connect(config.connection.sqlserver).then(() => sql.query(query)).then((result) => {
      sql.close();
      return result.recordset;
    }).catch((err) => console.log(err));
  },
};

I'm returning Promise so I can use then to presentation my data when I'm calling query function on a server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.