0

Firstly, I apologize if this has already been answered; I've been searching and searching but nothing I have found has given me that Eureka! moment...so I come here.

I am newish to Node and ES6 and am looking to abstract some of my functions/methods from my app.js file. Doing so...I have created a class like the below.

this.tryLogin(email, password)....

Always returns undefined.

I have tried:

in constructor()var self = this and set it to self.tryLogin()... to no avail

Experimented with using function UserHandler() {}.... and using prototypes.

My goal is to create a JS "class" where I have the ability to call it's own methods both INSIDE and OUTSIDE the class.

    class UserHandler {

    constructor() {
        this.aws_config = require("./aws-config");
    }

    redirectIfLoggedIn(request, response, next) {

        if (request.session.user && request.session.user.id_token) {

            if (request.query.redirect) {
                response.redirect(request.query.redirect);
            }
            else {
                response.redirect("/profile");
            }
        }
        else {
            next();
        }
    }

    handleLogin(request, response, next) {
        let form_data = request.body;
        let email = request.body.email;
        let password = request.body.password;
        let remember_me = request.body.remember_me;

        this.tryLogin(email, password).then(function () {

        }).catch(function(){

        });
    }

    tryLogin(username, password) {
        return new Promise(function(resolve, reject) {

        });
    }
    // Test function
    pingPromise(string) {
        return new Promise(function(resolve , reject) {
            if (string == "ping") {
                resolve("pong");
            }
            else {
                reject(Error("You must send 'ping'!"));
            }
        });
    }
}


module.exports = new UserHandler();

EDIT:

Error I am trying to solve TypeError: Cannot read property 'tryLogin' of undefined

I am initially calling the handleLogin function via ExpressJS as a middleware:

app.post(default_data.login_form_action, UserHandler.handleLogin, function (request, response) {});
Ray
  • 773
  • 9
  • 21
  • 1
    That looks about fine. What exactly doesn't work with *this* code, what error do you get? How/where *do* you call the methods from outside? – Bergi Jun 07 '17 at 19:39
  • Hey @Bergi, so here is the error I get `TypeError: Cannot read property 'tryLogin' of undefined`. In my app.js I am calling the function like this (using expressJS) app.post(default_data.login_form_action, UserHandler.handleLogin, function (request, response) {); – Ray Jun 07 '17 at 19:47
  • 1
    Yes, as suspected. It's not about how you call `tryLogin`, it's about the call to `handleLogin`. You need to bind it to your userhandler instance. `app.post(default_data.login_form_action, (req, res, next) => myUserHandler.handleLogin(req, res, next), function (request, response) {` – Bergi Jun 07 '17 at 19:49
  • Thanks @Bergi It appears that did it, however I'm still not 100% WHY...I kind of get it but I get lost fairly quickly with scopes and these "arrow" functions. – Ray Jun 07 '17 at 19:58

1 Answers1

0

I think you should return the result of calling this.tryLogin():

handleLogin(request, response, next) {
    let form_data = request.body;
    let email = request.body.email;
    let password = request.body.password;
    let remember_me = request.body.remember_me;

    return this.tryLogin(email, password).then(function () {

    }).catch(function(){

    });
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • Returning promises is a good practice, yes, but it looks like this is the end of the promise chain and he's going to call the `next` callback instead – Bergi Jun 07 '17 at 19:40