0

I was unable to retrieve the kitItems object when I try to access it outside the foreach loop.

This works

 for(i=0;i<customer.kit.length;i++){
        const kitItems = await KitItem.findAll({
            where :{
                kit_id:customer.kit[i].id
            }
        })       
       customer.kit[i].kitItems=kitItems                
    }
console.log(customer.kit[0].kitItems)   // this works 

This does not work

    customer.kit.forEach(async (kit,index) => {
        const kitItems = await KitItem.findAll({
            where :{
                kit_id:kit.id
            },
            raw:true
        })
       
       customer.kit[index].kitItems=kitItems         
    });
console.log(customer.kit[0].kitItems)   // this does not work 

What did I miss and better yet, how can I refactor this and use the new ES6/7 methods?

Biruk Belihu
  • 49
  • 1
  • 3

0 Answers0