我使用Sequelize with Express并从本教程开始: http://sequelizejs.com/articles/express
我希望用户拥有任何用户(朋友列表)
这是我的用户类:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
username : {type:DataTypes.STRING, allowNull:false,unique: true},
email : {type:DataTypes.STRING, allowNull:false,unique:true,validate:{isEmail : true},msg:"Email must be valid."},
password : {type:DataTypes.STRING,allowNull:false,validate:{len:[6,],msg:"Password length > 5."}},
score : DataTypes.INTEGER,
timeplay : DataTypes.BIGINT,
timeaverage : DataTypes.INTEGER,
nbrnotfoundword : DataTypes.INTEGER,
nbrnotfound : DataTypes.INTEGER
},
{
associate: function(models) {
User.hasMany(models.User)
}
})
return User
}
User.hasMany(models.User)给了我这个错误:
/home/damien/Documents/app/node_modules/sequelize/lib/associations/has-many.js:172
this.through.init(this.through.daoFactoryManager)
^
TypeError: Object true has no method 'init'
at module.exports.HasMany.injectAttributes (/home/damien/Documents/app/node_modules/sequelize/lib/associations/has-many.js:172:20)
at Mixin.hasMany (/home/damien/Documents/app/node_modules/sequelize/lib/associations/mixin.js:53:68)
at Object.sequelize.define.associate (/home/damien/Documents/app/models/user.js:18:11)
at module.exports.lodash.extend.sequelize (/home/damien/Documents/app/models/index.js:20:27)
at Array.forEach (native)
at Object.<anonymous> (/home/damien/Documents/app/models/index.js:18:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
寻求帮助。
答案 0 :(得分:0)
这很可能是因为在定义之前您使用了用户。
请改为尝试:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
username : {type:DataTypes.STRING, allowNull:false,unique: true},
email : {type:DataTypes.STRING, allowNull:false,unique:true,validate:{isEmail : true}, msg:"Email must be valid."},
password : {type:DataTypes.STRING,allowNull:false,validate:{len:[6,],msg:"Password length > 5."}},
score : DataTypes.INTEGER,
timeplay : DataTypes.BIGINT,
timeaverage : DataTypes.INTEGER,
nbrnotfoundword : DataTypes.INTEGER,
nbrnotfound : DataTypes.INTEGER
});
User.hasMany(User);
return User;
}
答案 1 :(得分:-1)
我发现了问题:
var Sequelize = require('sequelize-mysql').sequelize
而不是
var Sequelize = require('sequelize')