0

So when i call the code below the outputted value of name is always "testname" the default value. It should be 'newvalue;

// Constructor
function test(connection) {
    this.name = 'testname';

}

test.prototype.exec = function () {


    var Request = require('tedious').Request // this could be any event emitter;
    request = new Request("select id, name from somevals where id = 1", function (err, rowCount) {
        if (err) {
            console.log(err);
        } else {
            console.log(rowCount + ' rows');
        }
    });


    this.connection.execSql(request);

    request.on('row', function (columns) {
        this.name = 'newvalue'; //how do i  set the instance variable name so that it is visible to the calling module 
    });

};
// export the class
module.exports = test;

Calling looks like

var test = require("./Model/test");
        var b = new test(connection);
        b.exec();

    console.log(b.name);
Jules
  • 1,071
  • 2
  • 18
  • 37

1 Answers1

0

exec takes the callback

// Constructor
function test() {
    this.name = "testname";

}

test.prototype.exec = function (callback) {


    var Request = require('tedious').Request // this could be any event emitter;
    request = new Request("select id, name from somevals where id = 1", function (err, rowCount) {
        if (err) {
            console.log(err);
        } else {
            console.log(rowCount + ' rows');
        }
    });


    this.connection.execSql(request);

    request.on('row', function (columns) {
        callback(columns[1]); //how do i  set the instance variable name so that it is visible to the calling module 
    });

};
// export the class
module.exports = test;

callback is defined and test called as follows

function callback(name) {
    console.log(name);
}

    function executeStatement() {

    var beatle = require("./Model/test");
        var b = new test(connection);
        b.exec(callback);

    console.log(b.name);

    }
Jules
  • 1,071
  • 2
  • 18
  • 37