TypeError:user.comparePassword不是函数

时间:2016-07-12 20:26:16

标签: javascript node.js mongodb express

我不知道为什么会收到此错误。 TypeError:user.comparePassword不是函数。

我安装了所有依赖项。将APP链接到所有库。 我试图使用Postman检索数据,但它不起作用。我究竟做错了什么?

{
  "name": "e-commerce",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "^1.15.2",
    "connect-mongo": "^1.2.1",
    "cookie-parser": "^1.4.3",
    "ejs": "^2.4.2",
    "ejs-mate": "^2.3.0",
    "express": "^4.14.0",
    "express-flash": "0.0.2",
    "express-session": "^1.14.0",
    "mongoose": "^4.5.3",
    "morgan": "^1.7.0",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0"
  }
}


var express = require('express');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var ejs = require('ejs');
var engine = require('ejs-mate');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var flash = require('express-flash');
var MongoStore = require('connect-mongo/es5')(session);
var passport = require('passport');



var secret = require('./config/secret');
var User = require("./models/user.js");


var app = express();

mongoose.connect(secret.database, function(err){
  if (err) {
    console.log(err);
  } else {
    console.log("Connected to database");
  }
});
//Middleware
//add how to use static page
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: secret.secretKey,
  store: new MongoStore({url: secret.database, autoReconnect: true})
}));


app.use(flash());
app.use(passport.initialize());
app.use(passport.session());

app.engine('ejs', engine);
app.set('view engine', 'ejs');


var mainRoutes = require('./routes/main');
var userRoutes = require('./routes/user');
app.use(mainRoutes);
app.use(userRoutes);

app.listen(secret.port, function(err){
  if(err) throw err;
  console.log("Server running on port " + secret.port);
});




var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var User = require('../models/user');


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



passport.deserializeUser(function(id, done){
  User.findById(id, function(err, user){
    done(err, user);
  });
});


//middlewear

passport.use('local-login', new localStrategy({
  usernameField: 'email',
  passwordField: 'password',
  passReqToCallback: true
}, function(req, email, password, done){
  User.find({email: email}, function(err, user){
    if (err) return done(err);

    if(!user) {
      return done(null, false, req.flash('loginMessage', 'No user has been found'));
    }

    if(!user.comparePassword(password)){
      return done(null, false, req.flash('loginMessage', 'Opps! Wrong Password Pal'));
    }


    return done(null, user);
  });
}));

//custom function to validate
exports.isAuthenticated = function(req, res, next){
  if( req.isAuthenticated()) {
    return next();
  }
  res.redirect('/login');
};

3 个答案:

答案 0 :(得分:3)

究竟是什么.comparePassword

你从User得到./models/user.js(我认为它是一个mongo模型,但我们看不到它),然后你打电话给.find(如果它真的是Mongo它应该返回一个数组,一个结果你应该使用.findOne)然后你得到user变量的结果并尝试为你的结果调用一些方法.comparePassword(),你的情况肯定是undefined ......

尝试在console.log(user)之前添加user.comparePassword,您将看到确切的内容。

答案 1 :(得分:0)

  passport.use(new LocalStrategy({
      usernameField:"email",
      passwordField:"password",
      passReqToCallback:true,
 
   },
   function(req,username,password,done){
   User.findOne({
   email:username,
   },function(err,user){
    if(err)
    {   console.log(err);
        return done(err);}
       
        if(!user)
        return done(null,false,{message:"These user is not exits !"});
        user.authenticate(password, function(err,users,passwordError){
            console.log(users);
            if(passwordError){
                console.log(err)
              return  done(null,false,{message:"password is wrong"})
            } else if(users) {
                console.log(`correct password ${users}`)
              return   done(null,users);
               
            }
        })
    })
  }));

答案 2 :(得分:0)

我遇到了同样的问题,这个 hack 为我解决了

在你的中间件中替换

if(!user.comparePassword(password)){
      return done(null, false, req.flash('loginMessage', 'Opps! Wrong Password Pal'));
    }

const passwordOk = await User.findByCredentials(email, password);
      if (!passwordOk) {
        return done(null, false, { message: 'Invalid username or password' });
      }

并在 userSchema 之后的 ./models/user.js 页面中,添加此代码;

userSchema.statics.findByCredentials = async (email, password) => {
    const user = await User.findOne({ email })

    if (!user) {
        throw new Error('Unable to login')
    }

    const isMatch = await bcrypt.compare(password, user.password)

    if (!isMatch) {
        throw new Error('Unable to login')
    }

    return user
}

运行代码,应该可以了