gpt4 book ai didi

node.js - Sequelize : Object true has no method 'init'

转载 作者:行者123 更新时间:2023-12-03 22:43:14 25 4
gpt4 key购买 nike

我在 Express 中使用 Sequelize 并从本教程开始:
http://sequelizejs.com/articles/express

我希望用户拥有任何用户( friend 列表)

这是我的用户类:

    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)

谢谢你的帮助。

最佳答案

这很可能是由于您在定义之前使用了 User。

试试这个:

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;
}

关于node.js - Sequelize : Object true has no method 'init' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21271876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com