需要数据和盐参数
Data and salt arguments required
我正在尝试对我站点中的管理员密码进行哈希处理。我已经搜索并发现此错误是因为 null
或 undefined
我们要对其进行哈希处理的值。
这是我的代码,每当我 console.log(admin) 它 returns {}
,我不知道为什么。
adminSchema.pre('save', (next) => {
var admin = this;
console.log(admin)
bcrypt.hash(admin.password, 10, (err, hash) => {
if (err) return next(err);
admin.password = hash;
next();
});
});
var adminModel = mongoose.model('Admin', adminSchema);
module.exports = adminModel;
服务器端代码:
var adminModel = require('./../models/admins');
router.post('/register', (req, res) => {
var newAdmin = {
adminName: req.body.adminName,
faculty: req.body.faculty,
email: req.body.email,
password: req.body.password
}
adminModel.create(newAdmin, (err, admin) => {
if (err) {
console.log('[Admin Registration]: ' + err);
}
else {
console.log('[Admin Registration]: Done');
req.session.adminId = admin._id;
res.redirect('/admin/submitScore')
}
})
});
很遗憾,我找不到 console.log(admin) 为空的原因。如果有人能帮助我,我将不胜感激。
关键字 this
在箭头函数中使用时会改变作用域。 See more here。这在您的快速路线中不是问题,但在您的 mongoose 中间件中却是问题。将您的函数更改为不使用 this
或制作老式的 function(){}
我正在尝试对我站点中的管理员密码进行哈希处理。我已经搜索并发现此错误是因为 null
或 undefined
我们要对其进行哈希处理的值。
这是我的代码,每当我 console.log(admin) 它 returns {}
,我不知道为什么。
adminSchema.pre('save', (next) => {
var admin = this;
console.log(admin)
bcrypt.hash(admin.password, 10, (err, hash) => {
if (err) return next(err);
admin.password = hash;
next();
});
});
var adminModel = mongoose.model('Admin', adminSchema);
module.exports = adminModel;
服务器端代码:
var adminModel = require('./../models/admins');
router.post('/register', (req, res) => {
var newAdmin = {
adminName: req.body.adminName,
faculty: req.body.faculty,
email: req.body.email,
password: req.body.password
}
adminModel.create(newAdmin, (err, admin) => {
if (err) {
console.log('[Admin Registration]: ' + err);
}
else {
console.log('[Admin Registration]: Done');
req.session.adminId = admin._id;
res.redirect('/admin/submitScore')
}
})
});
很遗憾,我找不到 console.log(admin) 为空的原因。如果有人能帮助我,我将不胜感激。
关键字 this
在箭头函数中使用时会改变作用域。 See more here。这在您的快速路线中不是问题,但在您的 mongoose 中间件中却是问题。将您的函数更改为不使用 this
或制作老式的 function(){}