Route.post()需要回调函数,但在使用passport.js时得到了[object Undefined]

时间:2019-02-02 21:52:22

标签: node.js express passport.js

我得到Route.post() requires a callback function but got a [object Undefined]为错误。仅当我使用passport.js并使用我称为“会话”的控制器时,才会发生这种情况。

我已经看过SO了,到目前为止,大多数答案都只是OP在说他们得到了,或者他们的代码的某些部分正在创建一个本不应该的函数。也没有多少人知道这是否可能是护照配置问题。

当我尝试启动我的应用程序时会出现错误消息,因此我什至无法测试路由是否正常工作。

下面是我可以用来重现该错误的最少代码。

APP.JS

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const passport    = require("passport");


const app = express();
const config = require("./config/config");

mongoose.connect(config.db, {autoReconnect: true, useNewUrlParser: true}, (err) => {
    if (!err) console.log('MongoDB has connected successfully.');
});
mongoose.connection.on('error', function() {
  console.error('MongoDB Connection Error. Make sure MongoDB is running.');
});


app.use(bodyParser.urlencoded({extended: true}));

require('./controllers/passport')(passport);
app.use(passport.initialize());

app.use('/', require('./routes/user.js'));


app.listen(8080, () => {
    console.log('Auth server running express started on port 8080}.');
});

USER.JS

const express = require('express');
const router = express.Router();
const multer  = require('multer');
const upload = multer();

var signupFormCheck = require('../middleware/signup').signupFormCheck;

const sessions = require("../controllers/sessions");

router.post('/signup',
  upload.none(), 
  signupFormCheck,
  sessions.postSignup
  );

module.exports = router;

SESSIONS.JS

const passport = require('passport');

exports.postSignup = (req, res, next) => {
    passport.authenticate('signup', {
        successRedirect : '/verify', 
        failureRedirect : '/signup', 
        failureFlash : true,
        session: false
    })(req, res, next);
};

PASSPORT.JS

const config = require("../config/config");


const LocalStrategy   = require('passport-local').Strategy;
const uuidv4 = require('uuid/v4');



const User  = require('../models/user');


module.exports = function(passport) {

passport.use('signup', new LocalStrategy({
      usernameField: 'email',
      passReqToCallback : true
    },
    function(req, email, password, done) {
      User.findOne({ email: req.body.email }, function(err, existingUser) {
        if(err){
          console.log(err);
        }
        if (existingUser) {
          return done(null, false, {message: 'An account with that email address already exists.'});
        }

        var companyID = uuidv4();
        // edit this portion to accept other properties when creating a user.
        var user = new User({
          email: req.body.email,
          password: req.body.password, // user schema pre save task hashes this password
          role: config.defaultRole,
          companyID: companyID
        });

        user.save(function(err) {
          if (err) return done(err, false, {message: 'Error saving user.'});
          return done(null, user, {message: 'Welcome, time to get all setup now!'});
        });
      });
  })
);

};

如果需要其他任何代码,请询问,我将其添加到OP中。提前致谢!

0 个答案:

没有答案