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