How to bind this to MySQL QueryCallback?

18 views Asked by At

I'm using NodeJS and MySQL with the following class setup. I am grabbing a row from DB and then am going to assign properties/values to object. My issue is I'm not sure how to bind the "this" from the Provider object to the query callback.

class Provider {
    constructor(id){
        if(id){
            this.load(id);
        }
    }

    load(id){
        db.query("SELECT * FROM providers WHERE provider_id = " + id, function(err, res){
            if (err) {
                console.log(err);
            } else {
                // Here I'd like to have this refer to Provider and not query function
                console.log(this);
            }
        });
    }
};

I have tried:

let getById = db.query.bind(this);

But that doesn't seem to do the trick...

0

There are 0 answers