NODE.js - 如何在其他方法中使用方法

时间:2016-01-26 13:52:59

标签: javascript node.js

我想在同一个类中的其他方法中使用方法从db中计算一些数据,如下所示,但我只是得到了 错误

[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();
     });
   };
 };

任何人都可以给我一个提示或相同的线索如何以正确的方式做到这一点?

2 个答案:

答案 0 :(得分:5)

试试吧。您必须将 this 对象存储到变量中,以便在内部函数中使用它。 this根据执行上下文而有所不同。

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();
     });
   };
 };

答案 1 :(得分:1)

回调函数的上下文(this)在调用回调函数时确定。所以,你应该使用箭头函数或.bind(this)

使用箭头功能

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();
     });
   };
 };

使用.bind(...)方法

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 ) );
   };
 };