[英]Undefined function with bcrypt-nodejs
我正在使用bcrypt-nodejs在預保存函數中散列密碼。 我無法弄清楚為什么我繼續在bcrypt.hash的回調函數中收到錯誤'[TypeError:undefined is not a function]'。
var mongoose = require('mongoose'),
validate = require('mongoose-validate'),
bcrypt = require('bcrypt-nodejs'),
SALT_WORK_FACTOR = 10,
REQUIRED_PASSWORD_LENGTH = 8;
function validateStringLength (value) {
return value && value.length >= REQUIRED_PASSWORD_LENGTH;
}
var schema = mongoose.Schema({
email: {type: String,
required: true,
unique: true,
validate: [validate.email, 'is not a valid email address']
},
passHash: {type: String,
required: true,
validate: [validateStringLength, 'The password must be of min ' + REQUIRED_PASSWORD_LENGTH + ' characters length.']}
});
schema.pre('save', function (next) {
var self = this;
if (!self.isModified('passHash')) return next();
bcrypt.hash(self.passHash, SALT_WORK_FACTOR, null, function encryptedPassword (err, hash) {
if(err) console.log(err);
self.passHash = hash;
next();
});
});
schema.set('autoIndex', App.env !== 'production');
var Model = mongoose.model('User', schema);
module.exports = Model;
我檢查了傳遞的參數,它們是正確的。 返回的哈希值也為null。
有沒有人有類似的經歷? 我正在使用bcrypt-nodejs,因為bcrypt在使用npm進行安裝時給出了錯誤。
可重復使用:
var bcrypt = require('bcrypt-nodejs')
bcrypt.hash('foo', 10, null, function(err) {
if (err) throw err;
});
問題是salt需要是一個字符串(內部, bcrypt-nodejs
使用salt.charAt()
,而charAt()
未定義為數字)。
你可能想要這個:
bcrypt.hash(self.passHash, bcrypt.genSaltSync(SALT_WORK_FACTOR), ...);
(或異步版本, bcrypt.genSalt()
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.