admin管理员组文章数量:1194123
Can someone help me understand how to use instance methods in Sequelize? I've reviewed the documentation but have found it to be sparse. At present, I am trying to use setPassword and verifyPassword instance methods on my user model. When I try to call the code in the REPL, after having imported the user model and synced the DB, I get the following:
> models.User.setPassword('test');
TypeError: Object [object Object] has no method 'setPassword'
Here is the code for the user model:
var bcrypt = require('bcrypt');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('User', {
email: { type: DataTypes.STRING, unique: true, allowNull: false, validate: { isEmail: true } },
password: { type: DataTypes.STRING, allowNull: false},
firstName: {type: DataTypes.STRING},
lastName: {type: DataTypes.STRING},
companyName: {type: DataTypes.STRING},
admin: {type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false,},
forgotUrl: {type: DataTypes.STRING, unique: true},
forgotDate: {type: DataTypes.STRING},
lastLogin: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
paranoid: true,
instanceMethods: {
setPassword: function(password, done) {
return bcrypt.genSalt(10, function(err, salt) {
return bcrypt.hash(password, salt, function(error, encrypted) {
this.password = encrypted;
this.salt = salt;
return done();
});
});
},
verifyPassword: function(password, done) {
return bcryptpare(password, this.password, function(err, res) {
return done(err, res);
});
}
}
});
};
Can someone help me understand how to use instance methods in Sequelize? I've reviewed the documentation but have found it to be sparse. At present, I am trying to use setPassword and verifyPassword instance methods on my user model. When I try to call the code in the REPL, after having imported the user model and synced the DB, I get the following:
> models.User.setPassword('test');
TypeError: Object [object Object] has no method 'setPassword'
Here is the code for the user model:
var bcrypt = require('bcrypt');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('User', {
email: { type: DataTypes.STRING, unique: true, allowNull: false, validate: { isEmail: true } },
password: { type: DataTypes.STRING, allowNull: false},
firstName: {type: DataTypes.STRING},
lastName: {type: DataTypes.STRING},
companyName: {type: DataTypes.STRING},
admin: {type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false,},
forgotUrl: {type: DataTypes.STRING, unique: true},
forgotDate: {type: DataTypes.STRING},
lastLogin: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
paranoid: true,
instanceMethods: {
setPassword: function(password, done) {
return bcrypt.genSalt(10, function(err, salt) {
return bcrypt.hash(password, salt, function(error, encrypted) {
this.password = encrypted;
this.salt = salt;
return done();
});
});
},
verifyPassword: function(password, done) {
return bcrypt.compare(password, this.password, function(err, res) {
return done(err, res);
});
}
}
});
};
Share
Improve this question
asked Oct 17, 2013 at 17:54
surfearthsurfearth
3,1474 gold badges25 silver badges31 bronze badges
2
|
2 Answers
Reset to default 16Instance method can be used on specific element instances eg.
models.User.find(123).success( function( user ) {
user.setPassword('test');
});
You define the function as:
function(password, done)
Yet you don't supply the done parameter. Thus, the function leaves done as undefined and calling done() is executing an undefined function.
You could fix this in 3 ways:
- Default done to a noop function
function () {}
- Only return
done()
if done is defined - Supply a done callback when calling the instance function.
The alternative is to refactor it to return a promise which it resolves on completion.
本文标签: javascriptUsing Instance Methods in SequelizeStack Overflow
版权声明:本文标题:javascript - Using Instance Methods in Sequelize - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738470792a2088575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
this
in thesetPassword
instance method will fail to refer to the actual User instance. – Daniel C Commented Sep 12, 2015 at 0:18