#Pulling data from Strapi API

4 messages · Page 1 of 1 (latest)

tight steppe
#

I am struggling to pull data from a Strapi API data object.
api.js

export async function apiConnect() {
  let headersList = {
    "Authorization": "Bearer ####"
  }

  let response = await fetch("####", { 
    method: "GET",
    headers: headersList
  });

  let data = await response.text();
  return data;
}

index.astro

---
import { apiConnect } from '../../lib/api';

const strapiPage = await apiConnect();
---
<main>
  <p>Page loading...</p>
  { strapiPage }
</main>

This outputs an object of data. As shown in the image.

The problems come when I try to extract specific data from the object. Example…

{ strapiPage.data.attributes.clientCompany }

This returns an error Cannot read properties of undefined (reading 'attributes') (Second screen shot)

Help would be appreciated

proven jetty
tight steppe
#

Thanks for the responce but that doesn't resolve it…

  let data = await response.json();
  return data;
  { strapiPage.data.attributes.clientCompany }

Cannot read properties of undefined (reading 'clientCompany')

proven jetty
#

To access the data...

  { strapiPage.data[index].attributes.clientCompany } // with index the position of the element you want to access