I am building an application in Node using Hapi.JS.
I have a class for an authentication plugin that is giving me all sorts of problems. When I attempt to reference this
from within a method on the class, I get an error saying that this
is undefined. Why is this happening?
An excerpt:
class OAuth {
constructor () {}
register (server, err, next) {
this.server = server;
this.registerRoutes();
}
registerRoutes () {
console.log(this.server.route);
this.server.route([
{
method: 'POST',
path: '/oauth/token',
config: {
auth: false,
handler: function(request,reply){
console.log("test");
reply("test");
}
}
},
{
method: 'GET',
path: '/test',
config: {
auth: false,
handler: function(request,reply){
console.log("test");
reply("test");
}
}
}
]);
}
}
module.exports = new OAuth();
Elsewhere this is being called like:
const oauth = require('./oauth');
oauth.register(server);
Every time the register function is called, I receive this error:
TypeError: Cannot set property 'server' of undefined
Why on earth is my instance not working?