express - Can't acess variable outside forEach loop JavaScript - Stack Overflow

I want to get the allItems variable outside the function, but I just can get an empty array. The first

I want to get the allItems variable outside the function, but I just can get an empty array. The first console.log shows the array I wanted, but the second it's just [].

var allItems = [];    
orderPads.forEach(async  element => {
        
        element.orders_fk.forEach(async elem => {
            
            let itemsArray = await OrderedItem.find({ order_fk: elem });
            
            
            itemsArray.forEach(async e => {
                 
                let temp = e.toObject();
                const mesa = await Table.findById(element.mesa);
                temp.mesa = mesa.name;
                
                allItems.push(temp)
                console.log(allItems)
                
            })
        })
    });
console.log(allItems)

I've already looked for answers online, but nothing seems to work in this case. Thank you for the help!

I want to get the allItems variable outside the function, but I just can get an empty array. The first console.log shows the array I wanted, but the second it's just [].

var allItems = [];    
orderPads.forEach(async  element => {
        
        element.orders_fk.forEach(async elem => {
            
            let itemsArray = await OrderedItem.find({ order_fk: elem });
            
            
            itemsArray.forEach(async e => {
                 
                let temp = e.toObject();
                const mesa = await Table.findById(element.mesa);
                temp.mesa = mesa.name;
                
                allItems.push(temp)
                console.log(allItems)
                
            })
        })
    });
console.log(allItems)

I've already looked for answers online, but nothing seems to work in this case. Thank you for the help!

Share Improve this question asked Sep 18, 2020 at 22:27 Pedro Renato MelloPedro Renato Mello 881 silver badge8 bronze badges 1
  • 1 Async and forEach does not work together. Either use asyncForEach or use a simple for loop. – somethinghere Commented Sep 18, 2020 at 22:29
Add a ment  | 

1 Answer 1

Reset to default 4

Async functions don't do what you think they do in forEach. Foreach will wait for every loop, but since it won't return a Promise, you can't actually wait for all loops (the forEach) to be done - the method will just return the current null immediately. Then you log that once. You might notice that your last log is logged first, and your others later, indicating that forEach was pleted, but the loops pleted later, and your array only filled up after your first log.

Personally, I would suggest using a for loop itself instead, which does not actually take you out of the async flow:

const allItems = [];   

for( const element of orderPads ){
for( const elem of element.orders_fk ){

  const itemsArray = await OrderedItem.find({ order_fk: elem });
  
  for( const e of itemsArray ){
  
    const temp = e.toObject();
    const mesa = await Table.findById(element.mesa);
    temp.mesa = mesa.name;

    allItems.push(temp)
    console.log(allItems)
    
  }
  
}}

console.log(allItems)

for loops are core constructs in Javascript, while forEach is in essence a simplistic wrapper for a for loop in Arrays that was never intended for asynchronous use.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744829003a4595969.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信