title: Getting undefined in API call via Fetch - Express Nuxt
body: <p>I am extremely stuck on a task using Express, API, Fetch.</p>
  <p>I am using Nuxt + Shopify API endpoints to grab data such as orders like below</p>
  <p>This is my express API Endpoint.
  The results should return an array of objects ( orders )</p>
  <pre><code>const bodyParser = require('body-parser')
  const app = require('express')()
  const axios = require('axios')
  
  app.use(bodyParser.urlencoded({ extended: false }))
  app.use(bodyParser.json())
  
  app.post('/getJSON', async (req, res) =&gt; {
    const { customerID } = req.body
  
    const id = Buffer.from(customerID, 'base64')
      .toString('binary')
      .split('gid://shopify/Customer/')
      .pop()
    console.log('id is', id)
  
    const endpoint = `https://test.myshopify.com/admin/api/2020-07/customers/${id}/orders.json`
  
    try {
      const response = await axios.get(endpoint, {
        headers: {
          'Content-Type': 'application/json',
          'X-Shopify-Access-Token': '*****************'
        }
      })
  
      res.status(200).json(response.data.orders)
    } catch (error) {
      res.status(500).send(error)
    }
  })
  
  module.exports = app
  </code></pre>
  <p>Now, in my Nuxt store.js, I am using fetch to make a post requests to that endpoint above.</p>
  <pre><code>async function apiPost(endpoint, { data }) {
    await fetch(`/api${endpoint}/getJSON`, {
      method: 'POST',
      body: JSON.stringify(data),
      headers: {
        'Content-Type': 'application/json'
      }
    }).then(async (res) =&gt; {
      const contentType = res.headers.get('content-type')
  
      if (contentType.startsWith('text/html')) {
        return res.text()
      }
  
      if (contentType.startsWith('application/json')) {
        await res.json()
      }
    })
  }
  </code></pre>
  <p>When I console.log <strong>res.json()</strong>, it is a promise</p>
  <p><a href="https://i.stack.imgur.com/H18Uo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H18Uo.png" alt="enter image description here" /></a></p>
  <p>Since it is a promise, I wanted to see what the results were anyways.</p>
  <pre><code>res.json().then((resp) =&gt; {
      console.log('resp is', resp)
  })
  </code></pre>
  <p>Turns out the rest is an array of objects.</p>
  <p><a href="https://i.stack.imgur.com/odH3t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/odH3t.png" alt="enter image description here" /></a></p>
  <p>However, I do not know how to return this object correctly, as the &quot;ordersResponse&quot; below is always undefined !</p>
  <p>Below is a function calling the apiPost passing in the 'endpoint'.  However, the orderResponse is undefined.  I have tried solutions, but all of them end up as orderResponse being undefined.</p>
  <pre><code>async fetchOrders({ state, dispatch, commit }, payload) {
      try {
        const ordersResponse = await apiPost('/customer-orders', {
          data: { customerID: state.customer.id }
        })
  
        console.log('ordersResponse', ordersResponse) // **undefined**
      } catch (error) {
        console.error(error)
        throw error
      }
    },
  </code></pre>
  <p>Any help is appreciated</p>