Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
many to many relationship knex js

I am trying to fetch data from the database in this way: I want all the templates and those templates have an array of categories in it so

templateArray = [ 
  template1 = { name:string, ..., categories: array}
  template2 = { name:string, ..., categories: array}
]

the methode I am currently using

const findAll = async () => {
  let template = await getKnex()(tables.template).select();

  template.forEach(async (value) => {
    const categories = await getKnex()(tables.template)
      .select()
      .where(`${tables.template}.templateId`, value.templateId)
      .join(
        tables.template_category,
        `${tables.template}.templateId`,
        '=',
        `${tables.template_category}.template_id`,
      )
      .join(
        tables.category,
        `${tables.category}.catId`,
        '=',
        `${tables.template_category}.cat_Id`,
      );
    value.categories = categories;
  });

  return template;
};

currently I am doing this but the object doesn't seem to alter.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are not waiting for the promises to resolve. You should do:

await Promise.all(template.map(yourFunction))

instead of

template.forEach(yourFunction)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda