mercredi 27 septembre 2017

NodeJS - How do I access 'this' from a nested function in my object's constructor

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

    }
}




Aucun commentaire:

Enregistrer un commentaire