Nodejs error and MSSQL module

Asked

Viewed 345 times

1

Hello, I have an error in the nodejs MSSQL module for SQL Server connections. The connection occurs normal, the problem is time to perform the query, the function does not return me anything in callback.

Connection code:

var sql = require('mssql');

var config = {
user: 'user',
    password: 'password',
    server: 'ip',
    database: 'database',
    connectionTimeout: '5000',
    requestTimeout: '5000',
    options: {encrypt: true}
};

var pool = function(){
    var conn =  new sql.Connection(config, function(err){
        var request = new sql.Request(conn);
        //console.dir(request);
        return request;
    });
    return conn;
}

module.exports = function(){
    return pool;
}

DAO:

function CampanhaDAO(connection){
    this._connection = connection;
    //console.log(this._connection)
}

CampanhaDAO.prototype.getCampanhas = function(){
    var sql = "SELECT * FROM notificacao_campanha";

    this._connection.query(sql, function(err, recordset){

        console.log(recordset);
    });
};

module.exports = function(){
    return CampanhaDAO;
};

2 answers

0

Have you tried the recommended method in the documentation through async/await?

const executeQuery = async (query) => {
try {
	const pool = await sql.connect(config);
	const result = await pool.request().query(query);

	return result;
} catch (error) {
	console.error(error);
} finally {
	sql.close();
}
};

0


Change your method getCampanhas to work with promises and add the method connect before executing the query:

CampanhaDAO.prototype.getCampanhas = function(){
  var sql = "SELECT * FROM notificacao_campanha";
  var conn = this._connection.query(sql);

  new conn.Request().connect(function(err) {
    conn.query(sql).then(function(recordset){
      console.log(recordset);
      conn.close();
    }).catch(function(err) {
      console.error(err);
    });
  })
};
  • I tried, it just hangs while trying to do the query and does not return me anything in callback

  • I added a catch in my example, try so

  • { name: 'Connectionerror', message: 'Connection is closed. ', code: 'ECONNCLOSED' } Strange, it is closing the connection

  • @Joãovictor made one more change, try it like this

  • I managed to connect, the problem now is that it returns the error: "Could not find stored Procedure’S'"

  • @Joãovictor made one more change. Check if this works

  • Gave "Object "Promise" has in method 'query'

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.