我在带有上下文的节点中使用原型时遇到问题。
/**
* 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;
基本上节点快递框架调用httpGetList,并且“this”没有getList,因为“this”由于上下文而被表达,有没有办法改进我的代码以便正确地做到这一点,我猜它是否到了这个.getList 那么 this.db 也会脱离上下文吗?
任何帮助表示赞赏。