I struggle to create a dynamic navigation component. The navigation items should be consumed from an Rest API generated by the backend (in my case Django).
The API relevant address displays the list entries correctly (see below). However, I cannot find a proper way to call these items in my NextJS application. In my case, under components/Navigation.js
I try to consume the pages information from the function getAllPages (./api/wagtail.js)
export async function getAllPages() {
return await getRequest(`${API_URL}/v1/page_relative_urls/`);
}
In the browser the address
${API_URL}/v1/page_relative_urls/
displays the entries as expected and provides the below list:
{ "meta":
{ "total_count": 8 },
"items": [
{
"title": "Home Page",
"relative_url": "/"
},
{ "title": "About us",
"relative_url": "/about-us/"
},
...
]}
I tried then to use getAllPages in the compoenents/navigation.js:
import getAllPages from '../../api/wagtail.js'
const Navtest = () => {
return <div className={s.Root}>
<nav>
{getAllPages.items.map((link, index) => {
return (
<ul>
<Link href={link.title}>
<li key={index}>{link.title}</li>
</Link>
</ul>
);
})}
</nav>
</div>;
};
Navtest.propTypes = {};
Navtest.defaultProps = {};
export default Navtest;
This raises an error message TypeError: Cannot read properties of undefined (reading 'items')
What am I'm doing wrong?