title: How do I loop through an array item of json and make async API requests with certain json objects
body: <p>I apologize if my question seems kind of newbie, I'm very new to async programming and I'm still trying to figure everything out. I'm also trying to figure out how to ask good questions here so i tried to include the right information and a reproducable example. Thank you</p>
  <p>I have a simple online store i'm building that only has four products.</p>
  <p>When my server side client receives the users cart contents via JSON It receive the product_id, quantity, and price_id as an array of JSON data</p>
  <p>I'm trying to configure it to</p>
  <ol>
  <li><p>loop through each array item,</p>
  </li>
  <li><p>grab the product_id and price_id,
  send them to the stripe API to recieve the product object and price object respectively,</p>
  </li>
  <li><p>assign the &quot;unit_amount&quot; value from the price object and the &quot;images&quot; value and &quot;name&quot; value from the products object to their own variable</p>
  </li>
  <li><p>Put hose variables into an array and push the array to the lineItems array, which is to be used as a variable for the create-session request to Stripe Checkout</p>
  </li>
  </ol>
  <p>in short:</p>
  <p>Expected behavior: price_data object produced from each item in the array, array is sent with line_items object to session request to stripe checkout then user is redirected to checkout page with price_data items in checkout.</p>
  <p>current behavior: price data retrieved from Stripe api unable to be assigned to variable or passed to price_data. Returning TypeError: Cannot read property 'unit_amount' of undefined.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const stripe = require('stripe')('sk_test_51Hemg7ETi3TpMq6bUmiw1HoxERPmReLOT3YLthf11MEVh4xCmnsmxtFHZRlWpimoSnwHjmUOKNkOFsbr9lEEIybe00SQF71RtF');
  //This is a test secret key for a dummy stripe account, don't worry I'm not sharing anything sensitive 
  const express = require('express');
  const app = express();
  app.use(express.static('.'));
  const YOUR_DOMAIN = 'http://localhost:4242';
  
  
  
  app.post('/create-session', async(req, res) =&gt; {
        //Body of the POST request
        const cartContents = [{
            product_id: 'prod_IHb8dX3ESy2kwk',
            quantity: '2',
            price_id: 'price_1Hh1wcETi3TpMq6bSjVCf3EI'
          },
          {
            product_id: 'prod_IFIIyTO0fHCfGx',
            quantity: '2',
            price_id: 'price_1HeniJETi3TpMq6bPDWb3lrp'
          }
        ]
  
        //Array to push parsed data onto for line_items object in stripe session
        var lineItems = [];
  
        cartContents.forEach(async(item, index, array) =&gt; {
          //Retrieve price object from stripe API:
          const price = await stripe.prices.retrieve(
            item.price_id
          ).then(
            function(message) {
              console.log(message.unit_amount);
              //log the unit_amount value from price object
            },
            function(error) {
              console.log("Reject:", error);
            }
          );
          //retrieve product object from stripe API
          const product = await stripe.products.retrieve(
  
            item.product_id
          ).catch(err =&gt; console.error('error also'));
  
          console.log(product.name)
  
          // retrieve "name" and "images" from returned product object and assign to variable
          const productName = product.name;
          const productImage = product.images;
          //retrieve "unit_amount" from returned price object and assign to variable
          const productPrice = price.unit_amount
  
          //retrieve item quantity from cartContents object and assign to variable
          const productQuantity = item.quantity
  
          //Add variables to item and push to lineItems array
          lineItems.push({
            price_data: {
              currency: 'usd',
              product_data: {
                name: productName,
                images: [productImage],
              },
              unit_amount: productPrice,
            },
            quantity: productQuantity,
          })
        });
        const session = await stripe.checkout.sessions.create({
          payment_method_types: ['card'],
          line_items: lineItems,
          mode: 'payment',
          success_url: `http://localhost:5001/success.html`,
          cancel_url: `http://localhost:5001/cancel.html`,
        });
        res.json({
          id: session.id
        });</code></pre>
  </div>
  </div>
  </p>
  <p>Thank you</p>