Wednesday, 7 June 2017

Class calling own method within method NodeJS/ES6

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();



via Ray

No comments:

Post a Comment