Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

669
Views
No entiendo este error: "SequelizeEagerLoadingError"

Cuando pruebo mi ruta con el cartero, tengo este error: "SequelizeEagerLoadingError"

Intenté crear una configuración adicional para mi clave externa porque encontré esta solución ( https://forum.freecodecamp.org/t/nodejs-sequelize-eager-loading-error/480312/7 ) pero el problema es similar .

controladores/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, })) };

modelos/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;

modelos/post.js:

 module.exports = (sequelize, Sequelize) => { const Post = sequelize.define( "post", { userId: { type: Sequelize.INTEGER }, title: { type: Sequelize.STRING(40) }, }, ); return Post; };

modelos/configuración adicional.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 };
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Para obtener publicaciones junto con usuarios como modelos asociados, debe definir una asociación del modelo de post al modelo de user :

 user.hasMany(post, { foreignKey : 'userId', targetKey: 'id' }); post.belongsTo(user, { foreignKey : 'userId', targetKey: 'id' });
about 4 years ago · Juan Pablo Isaza Report

0

Sí ! es bueno !

 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' });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!