最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "TypeError: User.serializeUser is not a function" with passport.js - Stack Overflow

programmeradmin5浏览0评论

I am new to passport JS , and i was making my first program in passport, i had app.js in my main directory and user.js in the models directory inside the main directory. When i tried to run the mand node app.js i recieved the following error.

C:\Users\RAJ\Desktop\webD\auth\app.js:26
passport.serializeUser(User.serializeUser()); //encrypt
                            ^

TypeError: User.serializeUser is not a function
    at Object.<anonymous> (C:\Users\RAJ\Desktop\webD\auth\app.js:26:29)
    at Module._pile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:617:3

Following is my two files app.js and user.js.

app.js

var express               = require('express'),
mongoose              = require('mongoose'),
bodyParser            = require('body-parser'),
passport              = require('passport'),
User                  = require('./models/user'),
localStrategy         = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose');

mongoose.connect("mongodb://localhost/auth_demo");

 var app = express();
 app.set('view engine','ejs');
app.get("/",function(req,res){
res.render("home");
});
app.use(require("express-session")({
     secret : "some random shit",
     resave: false,
    saveUninitialized: false
}));
//setting up passport
app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(User.serializeUser()); //encrypt
passport.deserializeUser(User.deserializeUser()); //decrypt
app.get("/secret",function(req,res)
{
  res.render("secret");
});
 app.get("/register",function(req,res)
 {
    res.render("register");
 });
app.listen(8000,function(){
    console.log("server has started running");
});

user.js

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
    username: String,
    password: String
});

UserSchema.plugin(passportLocalMongoose);

module.export = mongoose.model("User",UserSchema);

Below is the list dependencies in package.json that I installed.

 "dependencies": {
    "body-parser": "^1.18.2",
    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "express-session": "^1.15.6",
    "mongoose": "^5.0.8",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^5.0.0"
  }

I am new to passport JS , and i was making my first program in passport, i had app.js in my main directory and user.js in the models directory inside the main directory. When i tried to run the mand node app.js i recieved the following error.

C:\Users\RAJ\Desktop\webD\auth\app.js:26
passport.serializeUser(User.serializeUser()); //encrypt
                            ^

TypeError: User.serializeUser is not a function
    at Object.<anonymous> (C:\Users\RAJ\Desktop\webD\auth\app.js:26:29)
    at Module._pile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:617:3

Following is my two files app.js and user.js.

app.js

var express               = require('express'),
mongoose              = require('mongoose'),
bodyParser            = require('body-parser'),
passport              = require('passport'),
User                  = require('./models/user'),
localStrategy         = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose');

mongoose.connect("mongodb://localhost/auth_demo");

 var app = express();
 app.set('view engine','ejs');
app.get("/",function(req,res){
res.render("home");
});
app.use(require("express-session")({
     secret : "some random shit",
     resave: false,
    saveUninitialized: false
}));
//setting up passport
app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(User.serializeUser()); //encrypt
passport.deserializeUser(User.deserializeUser()); //decrypt
app.get("/secret",function(req,res)
{
  res.render("secret");
});
 app.get("/register",function(req,res)
 {
    res.render("register");
 });
app.listen(8000,function(){
    console.log("server has started running");
});

user.js

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
    username: String,
    password: String
});

UserSchema.plugin(passportLocalMongoose);

module.export = mongoose.model("User",UserSchema);

Below is the list dependencies in package.json that I installed.

 "dependencies": {
    "body-parser": "^1.18.2",
    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "express-session": "^1.15.6",
    "mongoose": "^5.0.8",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^5.0.0"
  }
Share Improve this question asked Mar 5, 2018 at 5:36 user8378799user8378799 1
  • i got the error. module.exports will fix it, earlier i wrote module.export, it was a typoi got the error. module.exports will fix it, earlier i wrote module.export, it was a typo – user8378799 Commented Mar 5, 2018 at 13:24
Add a ment  | 

5 Answers 5

Reset to default 5

The export module is wrong is:

Is not module.export

module.export = mongoose.model("User",UserSchema);

but is module.exports

module.exports = mongoose.model("User", UserSchema);

You need to build your own serializeuser() and deserializeUser() functions.

serializeUser is used to store id of the user in the session,while deserializeUser is used to retrieve the user details of the user by fetching the id from the session and then fetching the whole user details from your database. Example:

passport.serializeUser(function(req, user, done) {
    done(null, user.user_id);
});



passport.deserializeUser(function(user_id, done) {
    getUserInfo(user_id).then(function(user) {
        return done(null, user);
    }, function(err) {
        return done(err,null);
    });
});

this app.js worked with me try it, I think it is the same syntax as yours

    var express = require("express");
    var mongoose = require("mongoose");
    var passport = require("passport");
    var bodyParser = require("body-parser");
    var localStrategy = require("passport-local");
    var passportLocalMongoose = require("passport-local-mongoose");
    var User = require("./models/user.js");
    var app = express();
    app.use(require("express-session")({
        secret: "this is a secret message",
        resave: false,
        saveUninitialized: false,
    }))
    app.set("view engine", "ejs");
    app.use(express.static(__dirname + "/public"));
    app.use(bodyParser.urlencoded({ extended: true })) 
    app.use(bodyParser.json())
    app.use(passport.initialize())
    app.use(passport.session())
    mongoose.connect("mongodb://localhost/auth", {useNewUrlParser: true, useUnifiedTopology: true});

    // passport.serializeUser(User.serializeUser())
    // passport.deserializeUser(User.desrializeUser())
    passport.serializeUser(User.serializeUser()); //encrypt
    passport.deserializeUser(User.deserializeUser()); //decrypt

    app.get("/", function(req, res){
        res.render("home")
    });

    app.get("/secret", function(req, res){
        res.render("secret")
    })

    app.listen(3000, function(){
        console.log("server started!")
    })

// Change these lines 
passport.serializeUser(User.serializeUser()); //encrypt
passport.deserializeUser(User.deserializeUser()); //decrypt

// With these lines
passport.serializeUser( (user, done) => {
  done(null, user.id);
});

passport.deserializeUser( (user, done) =>  {
  done(null, {
    provider: user.provider,
    id: user.provider_id
  });
});

See your directory tree very very carefully , and make sure you are importing the right file in app.js ! Also , make sure that the versions installed are these. It will work

发布评论

评论列表(0)

  1. 暂无评论