Monday, 17 April 2017

Route.get() requires callback functions but got a [object Undefined] - No existing solution helps

So I'm working on a project for a portfolio, and I'm working on a local signup that also features name, email, and some info. After checking online, I found this tutorial: https://scotch.io/tutorials/easy-node-authentication-setup-and-local

The local sign-up configuration, from my understanding, is what I can play with and tweak in order to get it to work for my code. The problem is that it doesn't work. When I try to debug, my most predominant errors are: TypeError: Cannot read property 'indexOf' of undefined Route.get() requires callback functions but got a [object Undefined]. This is the app.js(/server.js):

var express               =           require ("express");
var app                   =           express();
var mongoose              =           require("mongoose").set('debug', true);
var passport              =           require("passport");
var cookieParser          =           require('cookie-parser');
var LocalStrategy         =           require("passport-local");
var passportLocalMongoose =           require("passport-local-mongoose");
var User                  =           require("./models/user");

// REQUIRING ROUTES
var helpRoutes              =           require("./routes/help"),
    communityRoutes         =           require("./routes/community"),
    authRoutes              =           require("./routes/auth")(app, passport);

//DB
mongoose.connect(connection);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});

//APP SETUP
app.set("view engine", "ejs");
app.use(require("express-session")({
    secret: secret,
    resave: false,
    saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());


app.use(function(req, res, next) {
    res.locals.currentUser = req.user;
    next();
});

//USING ROUTES
app.use("/", authRoutes)(app, passport);
app.use("/help", helpRoutes);
app.use("/community", communityRoutes);

//==============================================
//SERVER START
//============================================== 

app.listen(process.env.PORT, process.env.IP, function(){
  console.log('Server Started');
});

passport.js configuration file:

var passport = require('passport'), 
    LocalStrategy = require('passport-local').Strategy,
    User = require("../models/user");

module.exports = function(user){

    //SERIALIZATION AND DESERIALIZATION REQUIRED FOR PERSISTENT LOGINS
    passport.serializeUser(function(user, done){
        done(null, user.id);
    });

    passport.deserializeUser(function(id, done){
        User.findById(id, function(err, user){
            done(err, user);
        });
    });

    //LOCAL SIGNUP
    passport.use("local-signup", new LocalStrategy({
        username: "username",
        password: "password",
        passReqToCallback: true,
        session: true},
        function(req, username, password, done){
            process.nextTick(function(){
                User.findOne({"username": username}, function(err, user){
                    if(err){
                        return done(err);
                    } 
                    if(user){
                        console.log(user, 'That username is already taken.');
                    } else {
                        var newUser = new User();
                        newUser.local.username = username;
                        newUser.local.password = password;
                        newUser.save(function(err){
                            if(err){
                                console.log(err);
                            }
                            return done(null, user);
                        });
                    }
                });
            });
        }    
    ));
};

I thought that maybe it's something to do with the routing, so I pasted the configurations in the app.js file, hoping to see if that works, at least, and the errors kept popping. This is the code I wanted to add:

passport.use("local-signup", new LocalStrategy({
    username: "username",
    password: "password",
    passReqToCallback: true,
    session: true},
    function(req, username, password, email, name, info, isAdmin, done){
        process.nextTick(function(){
            User.findOne({"username": username}, function(err, user){
                if(err){
                    return done(err);
                } 
                if(user){
                    console.log(user, 'That email is already taken.');
                } else {
                    var newUser = new User();
                    newUser.local.username = username;
                    newUser.local.password = newUser.generateHash(password);
                    newUser.local.name = name;
                    newUser.local.info = info;
                    newUser.local.isAdmin = isAdmin;
                    newUser.save(function(err){
                        if(err){
                            console.log(err);
                        }
                        return done(null, user);
                    });

I've searched all over here, but can't seem to find a solution, and the tutorial seems to work for others, but not me. I checked the upgrade to Express.js 4.0v, but the solution doesn't seem to lie there.



via Val S

No comments:

Post a Comment