简体   繁体   中英

“TypeError: passport.serialize is not a function” with Passport.js

I am relatively new to passport.js and have been following an online guide in conjunction with the documentation here: https://github.com/saintedlama/passport-local-mongoose . However, when I start my server in webstorm, I consistently get the error: TypeError: passport.serialize is not a function. I also saw this similar post: Passport.js .authenticate() throwing TypeError: undefined is not a function , but my problem is different from the one solved in that question. My relevant code is below:

user.js

var mongoose = require('mongoose'),
Schema = mongoose.Schema,
passportLocalMongoose = require('passport-local-mongoose');
var User = new Schema({
    username: String,
    password: String,
    studentID: String,
    grades: [{
        subject: String,
        grade: String
    }]
});
User.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', User);

app.js

var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var routes = require('./routes/index');
var LocalStrategy = require('passport-local').Strategy;
var passport = require('passport');
var request = require('request');
var cheerio = require('cheerio');
var User = require('./models/user');

var app = express();
passport.initialize();
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serialize(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

All help is appreciated!

There is no passport.serialize function. The correct is passport.serializeUser .

This is a code snipped from passport.

Authenticator.prototype.serializeUser = function(fn, req, done) {
...

and

Authenticator.prototype.deserializeUser = function(fn, req, done) {
...

I believe it is your problem.

Hope it can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM