我正在使用E-commerce Express API,并且正在使用'sequelize'模块进行数据库操作。 现在我只有两个表: 我的项目结构如下:
bin
-www
config
-database.js
models
-order.js
-user.js
routes
-user.js
.env
app.js
package.json
package-lock.json
database.js文件:
require('dotenv').config();
const Sequelize = require('sequelize');
module.exports = new Sequelize(
process.env.DATABASE_NAME,
process.env.DATABASE_USERNAME,
process.env.DATABASE_PASSWORD, {
port: process.env.DATABASE_PORT,
host: process.env.DATABASE_HOST,
dialect: 'mysql',
}
);
User.hasMany(Order);
Order.belongsTo(User, {constraints: false});
models-user.js
const db = require('../config/database');
const Sequelize = require("sequelize");
const User = db.define('user', {
username: {
type: Sequelize.STRING(32),
unique: true,
primaryKey: true,
allowNull: false
},
email: {
type: Sequelize.STRING(254),
unique: true,
allowNull: false,
},
password: {
type: Sequelize.STRING(128),
allowNull: false,
},
/*name: {
type: Sequelize.STRING
},
isAdmin: {
type: Sequelize.BOOLEAN
},*/
}, {
freezeTableName: true,
timestamps: false,
underScored: true
});
modules.exports=User;
models-order.js
const db = require('../config/database');
const Sequelize = require('sequelize');
const Order = db.define('order', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
dateTime: {
type: Sequelize.DATE,
allowNull: false
},
totalPrice: {
type: Sequelize.INTEGER,
allowNull: false
}
}, {
freezeTableName: true,
timestamps: false,
underscored: true
}
);
module.exports=Order;
启动应用程序时,我得到
TypeError: User.hasMany is not a function
at Object.<anonymous> (C:\Users\danfe\Documents\GitHub\USocks_Backend\config\database.js:23:6)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\Users\danfe\Documents\GitHub\USocks_Backend\models\user.js:1:74)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\Users\danfe\Documents\GitHub\USocks_Backend\config\passport.js:6:14)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
我做了一些谷歌搜索,这可能与循环依赖有关吗?但是我敢肯定,我有一些问题: 1.怎么了? 2.我是否正确定义关联? 3.定义关联的正确方法是什么?
答案 0 :(得分:1)
是的,它来自循环依赖。当您调用“ User.hasMany(Order)”时,尚未定义用户。要使用此关系之前,请在放置前调用“ hasMany”和“ belongTo”
答案 1 :(得分:0)
您的运行时不知道database.js中的用户和订单是什么。这类似于在不定义或初始化变量的情况下使用它。您需要使用
将用户和订单模型导入database.js中 const User = require("../models/user.js");
const Order = require("../models/order.js")
您还可以在模型本身内部定义关联。
在models/order.js
内部和module.exports之前-
Order.associate = function(models) {
Order.belongsTo(models.User,{
foreignKey:user_id
})
但是要使它起作用,您必须对属性进行初始化后继。
您应该使用sequelize-cli以正确的方式生成config.json,模型,迁移和种子。这样,您只需要在整个项目中导入单个db对象,就可以以正确的方式访问所有模型。