我创建了一个新的sails 项目并安装了sails-permission。首先它工作得很好。在创建与 Mysql 的连接并做风帆提升后,它会抛出一个错误,因为
error: Error: Consistency violation: A model (`passport`) references a
datastore which cannot be found ('mysql'). If this model definition
has an explicit
connection
property, check that it is spelled
correctly. If not, check your default
connection
(usually located in config/models.js).
模型 passport.js 位于node_modules/sails-permission/api/models
.
配置/connections.js
mysql: {
module : 'sails-mysql',
host : '127.0.0.1',
port : 3306,
user : 'review',
password : 'review',
database : 'reviews'
},
配置/models.js
module.exports.models = {
connection: 'mysql',
migrate: 'alter'
};
node_modules/sails-permission/api/models
var bcrypt = require('bcryptjs');
function hashPassword (passport, next) {
var config = sails.config.auth.bcrypt;
var salt = config.salt || config.rounds;
if (passport.password) {
bcrypt.hash(passport.password, salt, function (err, hash) {
if (err) {
delete passport.password;
sails.log.error(err);
throw err;
}
passport.password = hash;
next(null, passport);
});
}
else {
next(null, passport);
}
}
var Passport = {
attributes: {
password: { type: 'string', minLength: 8 },
provider : { type: 'alphanumericdashed' },
identifier : { type: 'string' },
tokens : { type: 'json' },
user: { model: 'User', required: true },
validatePassword: function (password, next) {
bcrypt.compare(password, this.password, next);
}
},
beforeCreate: function (passport, next) {
hashPassword(passport, next);
},
beforeUpdate: function (passport, next) {
hashPassword(passport, next);
}
};
module.exports = Passport;