Estherius
Estherius

Reputation: 9

NODE.js - how to use method in other method

I want to use method in other method in the same class to calc some data from db as below but Im only getting error

[TypeError: Object #<Query> has no method 'methodMaxLct']"
 exports.UserClass = function() {
 this.methodMaxLct = function(lct, callback) {
   var counting = Math.ceil(Math.pow(1.15, (lct - 1)) * 10) * 10;
   callback(counting);
   this.methodGetData = function(idu, callback) {
     connection = mysql.createConnection(dbconfig);
     connection.query(dataUserResources, [idu], function(err, results, fields) {
       if (err) throw err;
       if (results.length == 0) {
         callback = 0;
       } else {
         for (var i in results) {
           var dataU = results[i];
         }
         dataU.enMax = 30;
         var ap = this.methodMaxLct(dataU.lct, function(answer) {
           dataU.lctMax = answer;
         });
         callback(dataU);
       }
       connection.end();
     });
   };
 };

Can anyone give me a tip or same clue how to do that in right way?

Upvotes: 0

Views: 114

Answers (2)

Vlad Churakov
Vlad Churakov

Reputation: 348

The context (this) of the callback function is determined when the callback function is called. So, you should use either arrow function or .bind( this )

Using an arrow function

exports.UserClass = function() {
 this.methodMaxLct = function(lct, callback) {
   var counting = Math.ceil(Math.pow(1.15, (lct - 1)) * 10) * 10;
   callback(counting);
   this.methodGetData = function(idu, callback) {
     connection = mysql.createConnection(dbconfig);
     // use arrow function
     connection.query(dataUserResources, [idu], (err, results, fields) => {
       if (err) throw err;
       if (results.length == 0) {
         callback = 0;
       } else {
         for (var i in results) {
           var dataU = results[i];
         }
         dataU.enMax = 30;
         var ap = this.methodMaxLct(dataU.lct, function(answer) {
           dataU.lctMax = answer;
         });
         callback(dataU);
       }
       connection.end();
     });
   };
 };

Using .bind(...) method

exports.UserClass = function() {
 this.methodMaxLct = function(lct, callback) {
   var counting = Math.ceil(Math.pow(1.15, (lct - 1)) * 10) * 10;
   callback(counting);
   this.methodGetData = function(idu, callback) {
     connection = mysql.createConnection(dbconfig);
     connection.query(dataUserResources, [idu], function(err, results, fields) {
       if (err) throw err;
       if (results.length == 0) {
         callback = 0;
       } else {
         for (var i in results) {
           var dataU = results[i];
         }
         dataU.enMax = 30;
         var ap = this.methodMaxLct(dataU.lct, function(answer) {
           dataU.lctMax = answer;
         });
         callback(dataU);
       }
       connection.end();
     }.bind( this ) );
   };
 };

Upvotes: 1

Andrei CACIO
Andrei CACIO

Reputation: 2119

Try it like so. You have to store the this object into a variable so you can use it in an inner function. The this is different based on the execution context.

exports.UserClass = function() {
 var self = this;
 this.methodMaxLct = function(lct, callback) {
   var counting = Math.ceil(Math.pow(1.15, (lct - 1)) * 10) * 10;
   callback(counting);
   this.methodGetData = function(idu, callback) {
     connection = mysql.createConnection(dbconfig);
     connection.query(dataUserResources, [idu], function(err, results, fields) {
       if (err) throw err;
       if (results.length == 0) {
         callback = 0;
       } else {
         for (var i in results) {
           var dataU = results[i];
         }
         dataU.enMax = 30;
         var ap = self.methodMaxLct(dataU.lct, function(answer) {
           dataU.lctMax = answer;
         });
         callback(dataU);
       }
       connection.end();
     });
   };
 };

Upvotes: 5

Related Questions