Problem with read Property 'query' of Undefined

Asked

Viewed 165 times

0

I’m having trouble getting the result of a query in Nodejs bd -Sqlserver module - mssql

Error presented:

Typeerror: Cannot read Property 'query' of Undefined At formulasdao.getFormulas (C: xampp htdocs node_fa app models Formulasdao.js:6:22)

dbConnection.js

const sql = require('mssql');


var config = {
    user: 'user',
    password: 'password',
    server: 'ip',
    database: 'name_db',
    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;
}

Formulasdao.js

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

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

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

1 answer

0

The above error if gives on account of the form that was writing the code with arrowFunction where it has a global context, follows below the problem resolution:

//Antes
FormulasDAO.prototype.getFormulas = (callback)=>{
    this._connection.query('select * from usuario', callback);
}

//Depois

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

Browser other questions tagged

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