javascript - "TypeError: User.serializeUser is not a function"与 passport.js

标签 javascript node.js passport.js passport-local

我是 passport JS 的新手,我正在用 passport 制作我的第一个程序,我的主目录中有 app.js ,主目录中的 models 目录中有 user.js 。 当我尝试运行命令 node app.js 时,我收到以下错误。

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._compile (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

以下是我的两个文件 app.js 和 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"
  }

最佳答案

导出模块错误的是:

不是 module.export

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

但是是 module.exports

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

关于javascript - "TypeError: User.serializeUser is not a function"与 passport.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49104060/

相关文章:

javascript - 使用 javascript 从 <option> 获取值

javascript - Sequelize HasMany BelongsToMany 删除旧引用

javascript - Node js JSON 清理

node.js - passportjs facebook 将请求传递给回调

javascript - 从 Passport 访问 Passport 策略

node.js - 带有 Passport 的 Google Api 重定向

javascript - react-native 在模态内打开一个模态

Javascript 下载多个文件并连接成一个文件

javascript - 一个字符 react 后文本输入不集中?

javascript - Node JS 中的事件循环阻塞和异步编程