【发布时间】:2018-08-22 19:56:10
【问题描述】:
我收到了这个错误:
“用户不是构造函数类型错误”
我无法找出代码中有什么问题。任何建议表示赞赏!
routes/users.js
// Initialised user.js into users.js
// var User = require('../models/user');
if (errors) {
// code here is working
} else {
console.log('PASSED'); // logs "passed" msg successfully
var newUser = new User({
name: name,
email: email,
phone: phone,
password: password2
});
User.createUser(newUser, function (err, user) {
if (err) throw err;
console.log(user);
});
req.flash('success_msg', 'You had been succesfully registered! Try signin.');
res.redirect('/users/signin');
}
models/user.js
var mongoose = require('mongoose');
var bcrypt = require('bcrypt');
var UserScheme = mongoose.Schema({
name: {
type: String,
index: true
},
email: {
type: String
},
phone: {
type: String
},
password: {
type: String
}
});
var User = module.export = mongoose.model('User', UserScheme);
// encrypting password into hash
module.export.createUser = function(newUser, callback){
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
newUser.save(callback);
});
});
};
如果需要更多代码,我可以更新帖子。
【问题讨论】: