rishiag
rishiag

Reputation: 2274

ReferenceError: For request with hapi/Node.js

I have these two api handlers:

module.exports.loginWeb = function (request, reply, next) {
    if (request.auth.isAuthenticated) {
        return reply.redirect('/');
    }

    if(request.method === 'get'){
        reply.view("account/login", { title: 'Login' },  {layout: 'account'});

    }else{
        console.log(request);
        login(request, reply, next, 'web');
    }
};

module.exports.registerWeb = function(request, reply, next) {
    if (request.auth.isAuthenticated) {
        return reply.redirect('/');
    }

    if(request.method === 'get'){
        reply.view("account/register", { title: 'Register' },  {layout: 'account'});
    }else{
        // checkbox1 name should be changed
        if (!request.payload.checkbox1){
            return reply('Please accept Terms & Conditions')
        }

        register(request, reply, next, 'web');
    }
};

Register handler is working fine but login handler is giving error, ReferenceError: Uncaught error: request is not defined.

But request is getting printed just above the line successfully. What could be the problem? Also as I said, register handler is working alright with same type of code.

Edit: Adding login function

function login(register, reply, next, flag){
    if (!request.payload.email || !request.payload.password){
        return reply('Please enter email and password');
    }
    Account.findOne({email: request.payload.email[0]}, function(err, user){
        if (err){
            return reply(err)
        }
        else {
            if (!user){
                return reply('user doesnt exists')
            }
            else {
                //doing bunch of stuff
            }
        }
    })
}

Upvotes: 0

Views: 560

Answers (1)

Armand
Armand

Reputation: 24343

The problem is in the login function - request is not defined as an argument to this function. Perhaps you meant to write:

login(request, ...

where you have

login(register, ...

Upvotes: 2

Related Questions