0

I am trying to create a constructor for my Shelf class that sets its' shelfObjects instance variable to the results of a PostgreSQL query. The problem is that 'this' no longer refers to the instance of the class once we reach the callback function. How would I go about solving this problem in the right way?

class Shelf {

    constructor(displayNum, sideItemNum, callNum){
        this.displayNum = displayNum;
        this.sideItemNum = sideItemNum;
        this.callNumber = callNumber;

        var query = db.query("MY POSTGRESQL QUERY", function(err, result){
            this.shelfObjects = result.rows;
        });

    }
}
Elliot
  • 11
  • 3
  • TLDNR, `db.query(..., (err, result) => this.whatever = result.rows` – georg Sep 27 '17 at 19:56
  • You can assign this to a variable called self outside of db.query and call `self.shelfObjects = result.rows;` or JS's new arrow function.. – pro Sep 27 '17 at 20:09

0 Answers0