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

107
Views
Secuela findAll, EJS Para que la iteración muestre qué subcategorías corresponden a una categoría en un grupo de opciones y algunas selecciones

Quiero mostrar en algunos OPTGROUPS y un par de OPCIONES con una iteración, una categoría en cada optgroup y las subcategorías que están relacionadas con cada categoría (la relación se creó en mysql con un fk para category_id en la tabla de subcategorías) , y luego el modelo en mi proyecto con secuela

1

SUB CATEGORÍAS MODELO id: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true }, nombre: { type: dataTypes.STRING(50) }, fk_categoria: { type: dataTypes.INTEGER }

 CATEGORIES MODEL let cols = { id: { type: dataTypes.INTEGER, primaryKey: true, autoIncrement: true }, nombre: { type: dataTypes.STRING(50) } }; CATEGORIES ASSOCIATION WITH SUB CATEGORIES Categoria.associate = function(models){ Categoria.hasMany(models.SubCategoria,{ as: 'sub_categoria', foreignKey: 'fk_categoria' }) Categoria.hasMany(models.Producto,{ as: 'producto', foreignKey: 'fk_categoria' }) } SUB CATEGORIES ASSOCIATION WITH CATEGORIES SubCategoria.associate = function(models){ SubCategoria.belongsTo(models.Categoria,{ as: 'categoria', foreignKey: 'fk_categoria' }) SubCategoria.hasMany(models.Producto,{ as: 'productos', foreignKey: 'fk_sub_categoria' }) } AND THE PROMISES I WAS USING TO ASK FOR THE DATA, WITHOUT RELATION let promiseCategoria = db.Categoria.findAll(); let promiseSubCategoria = db.SubCategoria.findAll(); THE DATA TABLE [1]: https://i.stack.imgur.com/Mpzkw.png Sorry for the huge post but I needed to show everything to make you understand
about 4 years ago · Juan Pablo Isaza
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!