'Having trouble making asynchronous request and passing down the values throgh a promise chain

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 query the products and save them into an object, then pass down that object, filtering the ids and making the database query then saving that query into the aformentioned object, and repeat

this doesn't seem to be working... probably the querymodel 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.categories,
        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})
    }
}


Solution 1:[1]

You have an error in your idArrayGenerator function. You must use bracket notation when you access dynamically to an object property, i.e. use product[property] instead of product.property:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pablo Ramírez