[英]Sequelize: TypeError: User.hasMany is not a function
I am having this weird behavior. 我有这种奇怪的行为。 I have a User model, and a Client model. 我有一个用户模型和一个客户端模型。 User has many Clients. 用户有很多客户端。 I am using this docs: http://docs.sequelizejs.com/manual/tutorial/associations.html 我正在使用这个文档: http : //docs.sequelizejs.com/manual/tutorial/associations.html
When I run the server, Sequelize throws TypeError: User.hasMany is not a function 当我运行服务器时,Sequelize抛出TypeError:User.hasMany不是一个函数
Node version: 8.9.1 节点版本: 8.9.1
Dialect: postgres 方言: postgres
Database version: Postgres 10 数据库版本: Postgres 10
Sequelize version: 4.22.15 Sequelize版本: 4.22.15
The following files are all in the same folder 以下文件都在同一文件夹中
user.js model user.js模型
const Sequelize = require('sequelize');
const db = require('../index.js'); //This is an instance of new Sequelize(...)
const tableName = 'users';
const User = db.define('user', {
firstName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
lastName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
username: { //Username will be the email
type: Sequelize.STRING(80),
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: Sequelize.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
isAdmin: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
},
isActive: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, { tableName });
module.exports = User;
client.js model client.js模型
'use strict'
const Sequelize = require('sequelize');
const db = require('../index.js');
const instanceMethods = {
toJSON() {
const values = Object.assign({}, this.get());
return values;
},
};
const Client = db.define('clients', {
ibid: {
type: Sequelize.BIGINT,
allowNull: true,
defaultValue: null
},
firstName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
lastName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
agreementSigned: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
},
totalBalance: {
type: Sequelize.DECIMAL(11,2),
allowNull: true,
defaultValue: null
},
currentAllocation: {
type: Sequelize.STRING,
allowNull: true,
defaultValue: null
}
}, { instanceMethods });
module.exports = Client;
index.js models index.js模型
'use strict';
const User = require('./user')
const Client = require('./client');
User.hasMany(Client);
Client.belongsTo(User);
module.exports = {User, Client};
In the index.js, you haven't exported the new sequelize instance you have initialised and thus it is not available to the client.js and user.js! 在index.js中,您尚未导出已初始化的新sequelize实例,因此client.js和user.js无法使用它! the define function works on the sequelize instance object that you have created! define函数适用于您创建的sequelize实例对象!
Also there is a circular dependency in the project which might create problem! 此外,项目中存在循环依赖,这可能会产生问题!
I tried your code and changed the structure and it works now! 我尝试了你的代码并改变了结构,它现在正常工作! There might be another structures possible too but this one works for me! 可能还有其他结构,但这个对我有用!
1) Don't create a new instance of sequelize in the index.js rather create another file that will serve as initialisation for the sequelize instance! 1)不要在index.js中创建一个新的sequelize实例,而是创建另一个文件,作为sequelize实例的初始化!
sequelize_index.js (I have used this file for creating a base instance and then using this base instance in both the client and user models) sequelize_index.js(我使用此文件创建基本实例,然后在客户端和用户模型中使用此基本实例)
const Sequelize = require('sequelize');
const dbconfig=require('./dbconfig.json');
const sequelize = new Sequelize('postgres://' + dbconfig.USER + ":" + dbconfig.PASSWORD + "@" + dbconfig.HOST + ":5432/" + dbconfig.DB, {
host: dbconfig.HOST,
dialect: dbconfig.DIALECT,
pool: {
min: 0,
max: 5,
idle: 1000
}
});
module.exports={sequelize};
2) The client.js would look something like this! 2)client.js看起来像这样!
'use strict'
const Sequelize = require('sequelize');
const sequelize=require('./sequelize_index').sequelize;
const db = require('./index.js');
const instanceMethods = {
toJSON() {
const values = Object.assign({}, this.get());
return values;
},
};
const Client = sequelize.define('clients', {
ibid: {
type: Sequelize.BIGINT,
allowNull: true,
defaultValue: null
},
firstName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
lastName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
agreementSigned: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
},
totalBalance: {
type: Sequelize.DECIMAL(11,2),
allowNull: true,
defaultValue: null
},
currentAllocation: {
type: Sequelize.STRING,
allowNull: true,
defaultValue: null
}
}, { instanceMethods });
module.exports = Client;
3) User.js 3)User.js
const Sequelize = require('sequelize');
const sequelize=require('./sequelize_index').sequelize;
const db = require('./index.js'); //This is an instance of new Sequelize(...)
const tableName = 'users';
const User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
lastName: {
type: Sequelize.STRING(50),
allowNull: false,
validate: {
notEmpty: true
}
},
username: { //Username will be the email
type: Sequelize.STRING(80),
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: Sequelize.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
isAdmin: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
},
isActive: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, { tableName });
module.exports = User;
4) index.js 4)index.js
'use strict';
const sequelize = require('./sequelize_index').sequelize;
const User = require('./user')
const Client = require('./client');
User.hasMany(Client);
Client.belongsTo(User);
sequelize.sync({force: false}).then(function () {
console.log("Database Configured");
});
module.exports = {User, Client};
After running the index.js, the database would be created! 运行index.js后,将创建数据库!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.