Route.get()需要回调函数,但得到[对象未定义] - 没有现有的解决方案有帮助

时间:2017-04-17 17:33:51

标签: javascript node.js express authentication passport.js

所以我正在为一个投资组合项目工作,我正在进行本地注册,其中还包括姓名,电子邮件和一些信息。 在线查看后,我找到了这个教程: https://scotch.io/tutorials/easy-node-authentication-setup-and-local

根据我的理解,本地注册配置是我可以使用和调整的,以使其适用于我的代码。 问题是它不起作用。当我尝试调试时,我最主要的错误是:

  

TypeError: Cannot read property 'indexOf' of undefined Route.get() requires callback functions but got a [object Undefined].

这是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配置文件:

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

我认为这可能与路由有关,所以我将配置粘贴到app.js文件中,希望看看它是否有效,至少,错误一直在弹出。 这是我想添加的代码:

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

我在这里搜索过,但似乎无法找到解决方案,而且教程似乎适用于其他人,但不适合我。我检查了升级到Express.js 4.0v,但解决方案似乎并不存在。

0 个答案:

没有答案