显示错误:
TypeError: user.comparePassword is not a function
at User.findOne (/home/ubuntu/work/training/git new/eventica/controller/login.js:59:13)
at /home/ubuntu/work/training/git new/eventica/node_modules/mongoose/lib/model.js:4846:16
at /home/ubuntu/work/training/git new/eventica/node_modules/mongoose/lib/query.js:4283:12
at process.nextTick (/home/ubuntu/work/training/git new/eventica/node_modules/mongoose/lib/query.js:2776:28)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
我的问题在哪里?
我的JavaScript文件:
const User = require('../models/User.js');
var funcs = require('../functions');
var config = require('config');
Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider(config.get('ethereum.host')));
const fs = require("fs");
const path = require('path');
module.exports = function (app) {
app.get('/signin', function (req, res) {
res.render('signin', {
pageTitle: 'Welcome - ' ,
layout: false
});
//res.redirect('/signin');
});
app.get('/signup', function (req, res) {
res.render('signup', {
pageTitle: 'Welcome - ',
layout: false
});
});
app.post("/signin", function (req, res) {
sess = req.session;
var email = req.body.email;
var password = req.body.password;
const userData = {
email: email.trim(),
password: password.trim()
};
// find a user by email address
User.findOne({ email: userData.email }, (err, user) => {
//console.log ("user...", user);
if (err) { throw err; }
if (!user) {
return res.json({
status: 'Error',
msg: 'User details is not found for this email, please register....'
});
}
// check if a hashed user's password is equal to a value saved in the database
user.comparePassword(userData.password, (passwordErr, isMatch) => {
if (err) { throw err; }
if (!isMatch) {
return res.json({
status: 'Error',
msg: 'Incorrect email or password, please try again..'
});
//req.flash('error', 'Incorrect email or password, please try again..');
//return res.redirect('/login');
// const error = new Error('Incorrect email or password');
// error.name = 'IncorrectCredentialsError';
// console.log("Password doesnt match...")
//res.json(error);
} else {
sess.user = user;
return res.json({
status: 'OK',
msg: 'Login successful'
});
}
// return res.json({
// status:'OK',
// msg:'Login successfully'
// });
// res.redirect("/eventica");
});
});
});
模型/User.js
module.exports = mongoose.model('User', UserSchema);
UserSchema.methods.comparePassword = function comparePassword(password, callback) {
bcrypt.compare(password, this.password, callback);
};
// UserSchema.pre('save', function (next) {
// const user = this;
UserSchema.pre('save', function saveHook(next) {
const user = this;
// proceed further only if the password is modified or the user is new
if (!user.isModified('password')) return next();
return bcrypt.genSalt((saltError, salt) => {
if (saltError) { return next(saltError); }
return bcrypt.hash(user.password, salt, (hashError, hash) => {
if (hashError) { return next(hashError); }
// replace a password string with hash value
user.password = hash;
return next();
});
});
});