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

184
Visualizações
Trouble doing several asyncronous operations within promise chain while passing down all the data (NodeJs)

I'm implementing a controller function that will need to pass down a few database queries to the template engine, specifically I need to include the category and brand data of the products so that the user can filter further by category and brand, to do this I just...

  1. search all the product names that coincide with the search query
  2. save it inside an object and pass it down
  3. filter out the category ids and turn them into an array containing unique and ordered ids
  4. query the category model with the category id array
  5. save it inside an object and pass it down
  6. filter out the brand ids and turn them array containing unique and ordered ids
  7. query the brand model with the brand id array
  8. save it inside an object and pass it down
  9. render the HTML by passing down the object containing the product, category, and bran info

This doesn't seem to be working... probably the query model function is not being properly implemented and I'm getting back the promise object when querying the categories

find: (req,res)=>{
    Products.findAll({
        where: {
            name: {[Op.like]: `%${req.query.search_query}%`}
    }
        order: [["createdAt", "DESC"]]
    })
    .then((products)=>{
        let catalogData = {}
        catalogData['products'] = products
        return catalogData
    })
    .then((catalogData)=>{
        let categoryIdArray = idArrayGenerator(catalogData.products, 'categoryId')
        let categoryObject = queryModel(Categories, categoryIdArray)
        catalogData['categories'] = categoryObject
        return catalogData
    })
    .then((catalogData)=>{
        let brandIdArray = idArrayGenerator(catalogData.products, 'brandId')
        let brandObject = queryModel(Brands, brandIdArray)
        catalogData['brands'] = brandObject
        return catalogData
    })
    .then ((catalogData)=>{
        res.render(path.resolve(__dirname, '../views/main/catalog'), {
        title: "Catalogo | Vigilancia Argentina",
        products: catalogData.product,
        categories: catalogData.subcategories,
        brands: catalogData.brands,
    })})
    .catch(error => res.send(error))

    function idArrayGenerator(products, property){
        let Array = products.map(product => product.property)
        let ArraySanitized = lodash(Array).sortedUniq().sortBy()
        return ArraySanitized
    }

    function queryModel(Model, idArray){
        return Model.findAll({
            where:{ id: idArray }
        })
        .then ((data) => {return data})
    }
}
about 4 years ago · Juan Pablo Isaza
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