Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
Can someone clarify what is happening in this part of the code?

So I have part of code like this

ruter.get('/', async function (_, res) {
const [categories, items] = (await Promise.all([
    db.query('SELECT * FROM categories'),
    db.query('SELECT * FROM inventory'),
])).map(result => result.rows);

for (const category of categories) {
    category.items = items.filter(item => (item.categoryid === category.id));
}

I have database where I have table-categories and table-items categories table and items look like this:

enter image description here

enter image description here

What I don't understand is what happens with categories and inventory in this part of code ->

   db.query('SELECT * FROM categories'),
    db.query('SELECT * FROM inventory'),
])).map(result => result.rows);

Do they get joined? Also in this part

category.items = items.filter(item => (item.categoryid === category.id));

what item means here, there is no column named item in items table?

thank you!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This

const [categories, items] = (await Promise.all([
    db.query('SELECT * FROM categories'),
    db.query('SELECT * FROM inventory'),
])).map(result => result.rows);
  • makes two queries - one from categories, one from inventory
  • in parallel, with Promise.all
  • then takes the rows property from each query result and puts those into the categories and items variables.

They don't get joined - they're two entirely independent queries and get put into separate categories and items variables.

what item means here

With

items.filter(item =>

items is the result of the query SELECT * FROM inventory - item is a single row from that query.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda