Problem with result Undefined - nodejs

Asked

Viewed 108 times

-2

I am having problem to recover the data from db SQL Server with Node the same does not pull in any way the result of the query informed:

module mssql

dbConnectio

const sql = require('mssql');

const config = {
        user: 'user',
        password: 'password',
        server: 'id', 
        database: 'db_name',
        port: '1433',
        connectionTimeout: '5000',
        requestTimeout: '5000',
        options: {encrypt: true}
    };


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

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

Formulary

function FormulasDAO(connection) {
    this._connection = connection;
}

FormulasDAO.prototype.getFormulas = function(callback){
    this._connection.query('select * from USUARIO', callback);
}


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

inserir a descrição da imagem aqui

  • Question tag ta incomplete. I got thought it was Node.js only,but it was another. -1. Very confusing.

  • Thanks for the tip @Maurydeveloper

1 answer

0


Test like this:

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

If it works, do the test like this (maybe you need async/await only on connection)

var pool = function(){
    var conn = new sql.ConnectionPool(config, async function(err){
        var request = await new sql.Request(conn);
        //console.dir(request);
        return request;
    });
    return conn;
}
  • I did the test and the first one returns me the following error: Typeerror: this. _Connection.query is not a Function And the second code keeps returning Undefined

  • Have you been able to connect and get back the information somehow? Because so, your problem is probably of nodejs in relation to timing. When I started at the Node I picked up a lot about it, and I still do sometimes. Since it is a use of a lib 'mssql', it is more difficult to help, only if someone uses it as well. What I advise is to at least be able to make the connection and return the information, even if the code is a bit messy and later make the division into smaller functions.

  • Test one of these two ways: https://www.npmjs.com/package/mssql#asyncawait or https://www.npmjs.com/package/mssql#Promises. Once it works, it will be easier to split the code into smaller functions if needed.

  • 1

    Just to explain better, nodejs works asynchronously, that is, in the middle of a script if you access some file or internet connection, which requires a certain time to run, it continues the code. What is happening, therefore, is that sql.Request although called, has a delay, and as it is not waiting, it already continues the code and shows how Undefined, terminates the function and returning Undefined. That’s what the await test was for, to try to make him wait, but it didn’t work. Test some of the codes I mentioned that are in the lib doc, it will probably work.

Browser other questions tagged

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