I need to inject my db
object into the securityHandler
object, but I can't seem to work out how to do it.
In the securityHandler.authenticate
method I want to have access to all: db
, request
and response
.
I've tried this:
app.post('/api/login', securityHandler.authenticate(request, response, db) );
and
SecurityHandler.prototype.authenticate = function authenticate(request, response, db) {};
EDIT:
nane suggested passing the db object to the constructor of SecurityHandler:
var security = new SecurityHandler(db);
SecurityHandler itself looks like this:
function SecurityHandler(db) {
console.log(db); // Defined
this.db = db;
}
SecurityHandler.prototype.authenticate = function authenticate(request, response, next) {
console.log(this.db); // Undefined
};
The db object now exists in the constructor method, but for some reason is unaccessible in the authenticate method.