使用 beforeCreate 钩子继承创建模型
Sequelize create model with beforeCreate hook
我在创建之前定义了我的钩子:
module.exports = function (sequelize, DataTypes) {
var userSchema = sequelize.define('User', {
// define...
});
userSchema.beforeCreate(function (model) {
debug('Info: ' + 'Storing the password');
model.generateHash(model.password, function (err, encrypted) {
debug('Info: ' + 'getting ' + encrypted);
model.password = encrypted;
debug('Info: ' + 'password now is: ' + model.password);
// done;
});
});
};
当我创建模型时
User.create({
name: req.body.name.trim(),
email: req.body.email.toLowerCase(),
password: req.body.password,
verifyToken: verifyToken,
verified: verified
}).then(function (user) {
debug('Info: ' + 'after, the password is ' + user.password);
}).catch(function (err) {
// catch something
});
现在我从中得到的是
Info: Storing the password +6ms
Info: hashing password 123123 +0ms // debug info calling generateHash()
Executing (default): INSERT INTO "Users" ("id","email","password","name","verified","verifyToken","updatedAt","createdAt") VALUES (DEFAULT,'wwx@test.com','123123','wwx',true,NULL,'2015-07-15 09:55:59.537 +00:00','2015-07-15 09:55:59.537 +00:00') RETURNING *;
Info: getting ajJMvvevCvRDp5E7wK9MNuSRKjFpieGnO2WrETMFBKXm9p4Tz6VC. +0ms
Info: password now is: ajJMvvevCvRDp5E7wK9MNuSRKjFpieGnO2WrETMFBKXm9p4Tz6VC. +0ms
Info: after, the password is 123123 +3ms
似乎代码的每一部分都在工作。创建用户模式将调用 beforeCreate,它会正确生成密码的哈希码....除了它没有写入数据库!
我确定我遗漏了一段非常重要且显而易见的代码,但我就是找不到问题出在哪里 (啊啊)。感谢任何帮助!
挂钩在 Sequelize 中以异步方式调用,因此您需要在完成后调用完成回调:
userSchema.beforeCreate(function(model, options, cb) {
debug('Info: ' + 'Storing the password');
model.generateHash(model.password, function(err, encrypted) {
if (err) return cb(err);
debug('Info: ' + 'getting ' + encrypted);
model.password = encrypted;
debug('Info: ' + 'password now is: ' + model.password);
return cb(null, options);
});
});
(或者,您可以 return 承诺)
对于较新版本的 Sequelize,挂钩不再具有回调函数,而是承诺。因此代码看起来更像下面这样:
userSchema.beforeCreate(function(model, options) {
debug('Info: ' + 'Storing the password');
return new Promise ((resolve, reject) => {
model.generateHash(model.password, function(err, encrypted) {
if (err) return reject(err);
debug('Info: ' + 'getting ' + encrypted);
model.password = encrypted;
debug('Info: ' + 'password now is: ' + model.password);
return resolve(model, options);
});
});
});
我在创建之前定义了我的钩子:
module.exports = function (sequelize, DataTypes) {
var userSchema = sequelize.define('User', {
// define...
});
userSchema.beforeCreate(function (model) {
debug('Info: ' + 'Storing the password');
model.generateHash(model.password, function (err, encrypted) {
debug('Info: ' + 'getting ' + encrypted);
model.password = encrypted;
debug('Info: ' + 'password now is: ' + model.password);
// done;
});
});
};
当我创建模型时
User.create({
name: req.body.name.trim(),
email: req.body.email.toLowerCase(),
password: req.body.password,
verifyToken: verifyToken,
verified: verified
}).then(function (user) {
debug('Info: ' + 'after, the password is ' + user.password);
}).catch(function (err) {
// catch something
});
现在我从中得到的是
Info: Storing the password +6ms
Info: hashing password 123123 +0ms // debug info calling generateHash()
Executing (default): INSERT INTO "Users" ("id","email","password","name","verified","verifyToken","updatedAt","createdAt") VALUES (DEFAULT,'wwx@test.com','123123','wwx',true,NULL,'2015-07-15 09:55:59.537 +00:00','2015-07-15 09:55:59.537 +00:00') RETURNING *;
Info: getting ajJMvvevCvRDp5E7wK9MNuSRKjFpieGnO2WrETMFBKXm9p4Tz6VC. +0ms
Info: password now is: ajJMvvevCvRDp5E7wK9MNuSRKjFpieGnO2WrETMFBKXm9p4Tz6VC. +0ms
Info: after, the password is 123123 +3ms
似乎代码的每一部分都在工作。创建用户模式将调用 beforeCreate,它会正确生成密码的哈希码....除了它没有写入数据库!
我确定我遗漏了一段非常重要且显而易见的代码,但我就是找不到问题出在哪里 (啊啊)。感谢任何帮助!
挂钩在 Sequelize 中以异步方式调用,因此您需要在完成后调用完成回调:
userSchema.beforeCreate(function(model, options, cb) {
debug('Info: ' + 'Storing the password');
model.generateHash(model.password, function(err, encrypted) {
if (err) return cb(err);
debug('Info: ' + 'getting ' + encrypted);
model.password = encrypted;
debug('Info: ' + 'password now is: ' + model.password);
return cb(null, options);
});
});
(或者,您可以 return 承诺)
对于较新版本的 Sequelize,挂钩不再具有回调函数,而是承诺。因此代码看起来更像下面这样:
userSchema.beforeCreate(function(model, options) {
debug('Info: ' + 'Storing the password');
return new Promise ((resolve, reject) => {
model.generateHash(model.password, function(err, encrypted) {
if (err) return reject(err);
debug('Info: ' + 'getting ' + encrypted);
model.password = encrypted;
debug('Info: ' + 'password now is: ' + model.password);
return resolve(model, options);
});
});
});