I've hit a roadblock with a task involving Express, API, and Fetch. I'm utilizing Nuxt along with Shopify API endpoints to retrieve data such as orders.
Below is my express API Endpoint which should return an array of objects (orders).
const bodyParser = require('body-parser')
const app = require('express')()
const axios = require('axios')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// Rest of the code...
In my Nuxt store.js file, I am using fetch to send a post request to the endpoint mentioned above.
async function apiPost(endpoint, { data }) {
// Code snippet for making POST request...
}
When logging res.json(), it returns a promise.
https://i.sstatic.net/H18Uo.png
Despite being a promise, I tried to see the results anyhow.
// Code snippet for handling the promise returned by res.json()...
The results from the response end up being an array of objects.
https://i.sstatic.net/odH3t.png
The issue arises when trying to properly return this object as 'orderResponse' always remains undefined!
Here's a snippet where the 'apiPost' function is called with the 'endpoint'. However, regardless of the attempts made, 'orderResponse' stays undefined.
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
}
}
Any assistance would be greatly appreciated.