当前使用ExpressJS作为框架来创建一个小型CMS系统,但当前遇到了问题。我正在尝试为名为“角色”的表创建模型,但是当我将其添加到要包含的数据库文件中时,运行“ node app.js”后会出现以下错误
Unhandled rejection TypeError: Cannot read property 'id' of undefined
at Object.mapValueFieldNames (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\sequelize\lib\utils.js:232:19)
at Promise.try.then.then (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\sequelize\lib\model.js:2393:37)
at tryCatcher (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\promise.js:512:31)
at Promise._settlePromise (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\promise.js:569:18)
at Promise._settlePromise0 (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\promise.js:614:10)
at Promise._settlePromises (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\promise.js:694:18)
at _drainQueueStep (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\async.js:138:12)
at _drainQueue (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\async.js:131:9)
at Async._drainQueues (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\async.js:147:5)
at Immediate.Async.drainQueues (C:\Users\Ryahn\Documents\Projects\monkeys\node_modules\bluebird\js\release\async.js:17:14)
at runCallback (timers.js:810:20)
at tryOnImmediate (timers.js:768:5)
at processImmediate [as _immediateCallback] (timers.js:745:5)
如果我没有在数据库init文件中包含模型,那么它运行得很好。然后的想法是使用一种关系将两者配对。我不打算仅使用角色来分配权限。迁移文件的设置与模型相同。我假设通过匹配用户模型的设置方式,角色模型也将正常工作。但是现在我不确定发生了什么。
用户
'use strict';
const
Promise = require('bluebird'),
bcrypt = Promise.promisifyAll(require('bcrypt'))
;
module.exports = (sequelize, DataTypes) => {
const users = sequelize.define('users', {
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
username: {
type: DataTypes.STRING,
notEmpty: true,
unique: true
},
password: {
type: DataTypes.STRING,
notEmpty: true
},
role: {
type: DataTypes.INTEGER(2).UNSIGNED,
validate: {
isNumeric: true
}
},
steamid: {
type: DataTypes.STRING(17),
validate: {
len: 17
}
},
link: {
type: DataTypes.STRING,
validate: {
isUrl: true
}
},
last_login: DataTypes.DATE
},
{
hooks: {
beforeCreate: function(user, options) {
if (!user.changed('password')) {
return sequelize.Promise.reject('not modified');
}
return bcrypt.hash(user.password, 10).then(hash => {
return user.password = hash;
}).catch(err => {
return sequelize.Promise.reject(err);
});
},
beforeUpdate: function(user, options) {
if (!user.changed('password')) {
return sequelize.Promise.reject('not modified');
}
return bcrypt.hash(user.password, 10).then(hash => {
return user.password = hash;
}).catch(err => {
return sequelize.Promise.reject(err);
});
}
}
});
users.prototype.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(10));
};
users.prototype.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
};
users.sync({force: false}).then(() => {
return users.findOrCreate({
where: {
id: 1,
username: 'Admin'
},
defaults: {
username: 'Admin',
password: 'admin123',
role: 1,
steamid: parseInt(12345678901234567),
link: 'https://www.example.com/user_profile.php?userID=1171981'
}
});
});
return users;
};
角色
'use strict';
module.exports = (sequelize, DataTypes) => {
const roles = sequelize.define('roles', {
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
name: DataTypes.STRING,
slug: DataTypes.STRING
}, {
timestamps: false
});
let role = [];
role.push([
{ name: 'Admin', slug: 'admin'},
{ name: 'Staff', slug: 'staff'},
{ name: 'Trainer', slug: 'trainer'},
{ name: 'Captain', slug: 'captain'},
{ name: 'Monkey', slug: 'monkey'}
]);
roles.sync({force: false}).then(() => {
return roles.bulkCreate(role);
});
return roles;
};
初始化
const
config = require('../config/database'),
Sequelize = require('sequelize'),
sequelize = new Sequelize(config),
db = {}
;
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.users = require('./models/users')(sequelize, Sequelize);
db.roles = require('./models/roles')(sequelize, Sequelize);
module.exports = db;