I don't understand this error : "SequelizeEagerLoadingError"
I don't understand this error : "SequelizeEagerLoadingError"
当我用邮递员测试我的路线时,我遇到了这个错误:“SequelizeEagerLoadingError”
我曾尝试为我的外键创建一个额外的设置,因为我找到了这个解决方案 (https://forum.freecodecamp.org/t/nodejs-sequelize-eager-loading-error/480312/7),但问题很相似。
controllers/post.js :
exports.getAllPost = (req, res, next) => {
/*** on récupère tous les posts ***/
Post.findAll({
include: [{
model: User,
attributes: ['username']
}],
})
/*** si tout est ok ***/
.then(post => res.status(200).json({
post
}))
/*** sinon on envoie une erreur ***/
.catch(error => res.status(400).json({
error,
}))
};
models/index.js :
require('dotenv').config();
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const { applyExtraSetup } = require('./extra-setup');
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.users = require("./user.js")(sequelize, Sequelize);
db.posts = require("./post.js")(sequelize, Sequelize);
db.comments = require("./comment.js")(sequelize, Sequelize);
applyExtraSetup(sequelize);
module.exports = db;
models/post.js :
module.exports = (sequelize, Sequelize) => {
const Post = sequelize.define(
"post", {
userId: {
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING(40)
},
},
);
return Post;
};
models/extra-setup.js :
function applyExtraSetup(sequelize) {
const { user, post, comment} = sequelize.models;
user.hasMany(post, {
foreignKey : 'userId',
targetKey: 'id'
});
post.hasMany(comment, {
foreignKey : 'postId',
targetKey: 'id'
});
user.hasMany(comment, {
foreignKey : 'userId',
targetKey: 'id'
});
}
module.exports = { applyExtraSetup };
为了将帖子与用户一起作为关联模型,您需要定义从 post
模型到 user
模型的关联:
user.hasMany(post, {
foreignKey : 'userId',
targetKey: 'id'
});
post.belongsTo(user, {
foreignKey : 'userId',
targetKey: 'id'
});
是的!很好!
db.users.hasMany(db.posts, {
foreignKey : 'userId',
targetKey: 'id'
});
db.posts.belongsTo(db.users, {
foreignKey : 'userId',
targetKey: 'id'
});
db.comments.belongsTo(db.users, {
foreignKey : 'userId',
targetKey: 'id'
});
db.comments.belongsTo(db.posts, {
foreignKey : 'postId',
targetKey: 'id'
});
当我用邮递员测试我的路线时,我遇到了这个错误:“SequelizeEagerLoadingError”
我曾尝试为我的外键创建一个额外的设置,因为我找到了这个解决方案 (https://forum.freecodecamp.org/t/nodejs-sequelize-eager-loading-error/480312/7),但问题很相似。
controllers/post.js :
exports.getAllPost = (req, res, next) => {
/*** on récupère tous les posts ***/
Post.findAll({
include: [{
model: User,
attributes: ['username']
}],
})
/*** si tout est ok ***/
.then(post => res.status(200).json({
post
}))
/*** sinon on envoie une erreur ***/
.catch(error => res.status(400).json({
error,
}))
};
models/index.js :
require('dotenv').config();
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const { applyExtraSetup } = require('./extra-setup');
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.users = require("./user.js")(sequelize, Sequelize);
db.posts = require("./post.js")(sequelize, Sequelize);
db.comments = require("./comment.js")(sequelize, Sequelize);
applyExtraSetup(sequelize);
module.exports = db;
models/post.js :
module.exports = (sequelize, Sequelize) => {
const Post = sequelize.define(
"post", {
userId: {
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING(40)
},
},
);
return Post;
};
models/extra-setup.js :
function applyExtraSetup(sequelize) {
const { user, post, comment} = sequelize.models;
user.hasMany(post, {
foreignKey : 'userId',
targetKey: 'id'
});
post.hasMany(comment, {
foreignKey : 'postId',
targetKey: 'id'
});
user.hasMany(comment, {
foreignKey : 'userId',
targetKey: 'id'
});
}
module.exports = { applyExtraSetup };
为了将帖子与用户一起作为关联模型,您需要定义从 post
模型到 user
模型的关联:
user.hasMany(post, {
foreignKey : 'userId',
targetKey: 'id'
});
post.belongsTo(user, {
foreignKey : 'userId',
targetKey: 'id'
});
是的!很好!
db.users.hasMany(db.posts, {
foreignKey : 'userId',
targetKey: 'id'
});
db.posts.belongsTo(db.users, {
foreignKey : 'userId',
targetKey: 'id'
});
db.comments.belongsTo(db.users, {
foreignKey : 'userId',
targetKey: 'id'
});
db.comments.belongsTo(db.posts, {
foreignKey : 'postId',
targetKey: 'id'
});