2

I deal with nodejs and i cannot figure out, why i cannot call class instance from another class function. Here is my code. Please help.

var StationDAO = require('./StationDAO.js');
var StationSearchCriteria = require('./StationSearchCriteria.js');

function GasStationService(lat, long, zoom) {
this.latitude = lat;
this.longitude = long;
this.zoom = zoom;
this.zoomToKilometers = 0.009;

/* ......... */

this.minLat = function () {
    return this.latitude - this.zoomToKilometers;
};

this.minLong = function() {
    return this.longitude - this.zoomToKilometers;
};

this.maxLat = function() {
    return this.latitude + this.zoomToKilometers;
};

this.maxLong = function() {
    return this.longitude + this.zoomToKilometers;
};

this.criteria = new StationSearchCriteria(this.minLat(), this.minLong(),     this.maxLat(), this.maxLong());
this.conn = new StationDAO(criteria); // why this doesnt work
4
  • Is StationDAO module defined as a single function bound to module.exports?
    – ilmirons
    Commented Mar 13, 2014 at 10:06
  • It is class accessing to Elastic search DB and it is module.exports
    – TomHonner
    Commented Mar 13, 2014 at 11:13
  • Is there any async code in StationDAO? (maybe something is not yet initialized when you are invoking it).
    – ilmirons
    Commented Mar 13, 2014 at 11:22
  • yes, there is one function, but it isnt called.
    – TomHonner
    Commented Mar 13, 2014 at 11:25

1 Answer 1

1

you are passing criteria which is not defined i assume you wanted to pass this.criteria isntead.

this.criteria = new StationSearchCriteria(this.minLat(), this.minLong(), this.maxLat(), this.maxLong());
this.conn = new StationDAO(this.criteria); //this should work
4
  • if ` criteria ` is not defined then how this.criteria is existing ? Commented Mar 13, 2014 at 10:30
  • this.criteria is not the same as criteria. I seriously dont understand your comment.
    – 0x_Anakin
    Commented Mar 13, 2014 at 11:27
  • 'this' pointer refers the current object's variable, in the problem statement 'criteria' is a variable which is the instance of StationSearchCriteria class and is already initialized. Commented Mar 13, 2014 at 11:48
  • 1
    +1'd this to bring it to zero since I think someone downvoted it not understanding it. Or maybe I am not understanding. I don't know why someone thinks that foo and this.foo would be the same thing. Commented Mar 13, 2014 at 14:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.