我总是得到SequelizeEagerLoadingError的每个API,我调用,其中包括另一个模型。
index.js
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const db = {};
const basename = path.basename(__filename);
const sequelize = require('../config/db');
fs.readdirSync(__dirname)
.filter(function (file) {
return (
file.indexOf('.') !== 0 &&
file !== basename &&
file.slice(-3) === '.js' &&
file !== 'associations.js' // Exclude the associations.js file from the loop
);
})
.forEach(function (file) {
var model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
console.log('Loaded model:', model.name);
});
const associations = require('./associations');
associations(sequelize);
Object.values(db).forEach((model) => {
if (model.associate) {
model.associate(db);
}
});
db.Sequelize = Sequelize;
db.sequelize = sequelize;
module.exports = db;
字符串
associations.js
module.exports = (sequelize) => {
const User = sequelize.models.users;
const Property = sequelize.models.properties;
const Like = sequelize.models.likes;
const Follow = sequelize.models.follows;
const Wallet = sequelize.models.wallets;
const Client = sequelize.models.clients;
const Bid = sequelize.models.bids;
console.log(Property);
console.log(User);
User.hasMany(Property, { foreignKey: 'createdBy', as: 'properties' });
User.hasMany(Like, { foreignKey: 'userId' });
User.hasOne(Wallet, { foreignKey: 'userId', as: 'wallet' });
User.hasMany(Follow, { foreignKey: 'userId', as: 'follows' });
Property.belongsTo(User, { foreignKey: 'createdBy', as: 'user' });
Property.hasMany(Like);
Property.hasMany(Bid);
Client.hasMany(Like, { foreignKey: 'userId' });
Client.hasMany(Follow, { foreignKey: 'userId', as: 'follows' });
Client.hasOne(Wallet, { foreignKey: 'userId', as: 'wallet' });
};
型
propertyModel.js(userModel.js类似)
const { v4: uuidv4 } = require('uuid');
const { DataTypes } = require('sequelize');
module.exports = (sequelize) => {
const Property = sequelize.define("properties", {
id: { type: DataTypes.UUID, defaultValue: () => uuidv4(), primaryKey: true, allowNull: false },
country: { type: DataTypes.STRING },
street: { type: DataTypes.STRING },
apartment: { type: DataTypes.STRING },
city: { type: DataTypes.STRING },
state: { type: DataTypes.STRING },
postal: { type: DataTypes.STRING },
name: { type: DataTypes.STRING },
type: { type: DataTypes.STRING },
structure: { type: DataTypes.STRING },
description: { type: DataTypes.TEXT },
price: { type: DataTypes.DOUBLE },
experience: { type: DataTypes.INTEGER },
longitude: { type: DataTypes.DOUBLE },
latitude: { type: DataTypes.DOUBLE },
image: { type: DataTypes.JSON },
createdBy: { type: DataTypes.UUID },
createdAt: { type: DataTypes.DATE },
updatedBy: { type: DataTypes.UUID },
updatedAt: { type: DataTypes.DATE },
});
return Property;
};
型
property.Api
Property.findAll({
attributes: [
'*',
],
include: [
{
model: User,
as: 'user',
},
]
})
型
一个简单的findAll可以正常工作。EagerLoadingError [SequelizeEagerLoadingError]:用户未与属性关联!我做错了什么?
1条答案
按热度按时间vs3odd8k1#
找到解决办法了。错误如下。
SequelizeEagerLoadingError with Sequelized
在将关联分配给模型之后,应该从索引文件中提取模型。