Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get undefined or Promise { <pending> } trying to push to an array

I have problems pushing to an array in a SSR page in the below code.

    let categories = []

    categories = await axios.get(`http:/.../${price_list_name}`).then(res => {
        return res.data
    })

    const child_categories = categories.related.category_childs.hits.hits
    const childs_en_names = []
    if (child_categories.length > 0) {
        for (var doc in child_categories) {
            childs_en_names.push(child_categories[doc]._source.en_name)
        }
    }

    const get_products = async (en_name) => {
        await axios.get(`http://.../${en_name}`).then(res => {
            let data = {
                "en_name": en_name,
                "products": res.data.related.childs_products
            }
            return data
        })
    }

    const products = await Promise.all(childs_en_names.map(en_name => get_products(en_name))) 


    // logging
    // console.log(categories.products, 'props')
    console.log(products, 'products')

cosole.log(products, ‘products’) returns me undefined or Promise { }. I have searched alot but haven’t been successfull to make it work.

any help would be highly appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re not returning anything from get_products. There’s no point in using async/await if you’re going to use then too. It sort of defeats the point.

const get_products = async (en_name) => {
  const res = await axios.get(`http://.../${en_name}`);
  const data = {
    en_name: en_name,
    products: res.data.related.childs_products
  }
  return data;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading