Bradley Weston
Bradley Weston

Reputation: 425

JS function prototype out of context node express

I have an issue using prototype in node with context.

/**
 * Constructor.
 * 
 * @param   object  opts        The options for the api.
 * @param   object  config      The application's configuration.
 * @param   object  db          The database handler.
 * @return  void
 */
var clientModel = function ( opts, config, db )
{
    this.opts = opts;
    this.config = config;
    this.db = db;
};

/**
 * Get a list of items.
 * 
 * @param   function    cb  Callback function.
 * @return  void
 */
clientModel.prototype.getList = function( cb )
{
    this.db.query(
        "SELECT FROM " + this.db.escape("client"),
        function ( err, rows, fields )
        {
            if( err.code && err.fatal )
            {
                cb(
                {
                    message: "SQL error locating client."
                });
                return;
            }

            if(! rows.length )
            {
                cb(
                {
                    message: "Unable to locate client."
                });
                return;
            }

            cb( false, rows, fields );
        });
};

/**
 * Default http request for getting a list of items.
 * 
 * 
 * @param   object  req     The http request.
 * @param   object  res     The http response.
 * @return  void
 */
clientModel.prototype.httpGetList = function ( req, res )
{
    this.getList( function ( err, rows, fields )
    {
        res.end("Got a list");
    });
}


// - Append model to output.
module = module.exports = clientModel;

Basically node express framework calls httpGetList and "this" doesn't have getList due to "this" being express due to context, is there any way of improving my code in order to do this correctly, I am guessing if it got to the this.getList then this.db would also be out of context?

Any help appreciated.

Upvotes: 2

Views: 481

Answers (2)

WouterH
WouterH

Reputation: 1356

You can bind your functions to an object, so that no matter how they get called, this will be as you expect it. You can find more information here.

You can bind the methods in your constructor. The underscore library has an useful bindAll method to help you with that.

Upvotes: 2

Kyle Needham
Kyle Needham

Reputation: 3389

I suggest you make the instance inside the module and export the functions that handle the requests.

/**
 * Exports.
 * 
 * @param   object  opts        The options for the api.
 * @param   object  config      The application's configuration.
 * @param   object  db          The database handler.
 * @return  void
 */
module = module.exports = function ( opts, config, db )
{
    var instance = new clientModel( opts, config, db );

    return {
        /**
         * Default http request for getting a list of items.
         * 
         * 
         * @param   object  req     The http request.
         * @param   object  res     The http response.
         * @return  void
         */
        httpGetList : function ( req, res )
        {
            instance.getList( function ( err, rows, fields )
            {
                res.end("Got a list");
            });
        }

    };
};

Upvotes: 1

Related Questions