I am continuously encountering the issue stating 'TypeError: events.map is not a function'

Being completely new to Next.js and Strapi, I've been grappling with this issue for the past couple of days.

While my index pages are successfully fetching data from my Strapi V4 API backend, my detail pages/[slug].js keep throwing the error 'events.map is not a function', along with other errors when attempting workarounds.

Here's the code snippet for my [slug].js

export default function EventPage({ evt }) {
    return (
        <Layout>
            <div className={classes.container}>
                <GridContainer>
                    <GridItem xs={12} sm={12} md={6} lg={6} >
                        <Card blog>
                            <CardHeader image>
                                <a href="#">
                                    <img key={evt.id}
                                        src={evt.image.formats.medium.url
                                        ? evt.attributes.image.data.attributes.formats.medium.url
                                        : '/img/image.webp'}
                                        alt="..." 
                                    />
                                </a>
                            </CardHeader>
                            <CardBody>
                                <Info>
                                    <h6 className={classes.cardCategory}>{ evt.name }</h6>
                                </Info>
                                <div className={classes.cardDescription}>
                                    { evt.description }
                                </div>
                            </CardBody>
                        </Card>
                    </GridItem>
                </GridContainer>
            </div>
        </Layout>
    )
}

export async function getStaticPaths() {
    const res = await fetch(`${API_URL}events`)
    const events = await res.json()
  
    const paths = events.map((evt) => ({
      params: { slug: evt.slug },
    }))
  
    return {
      paths,
      fallback: true,
    }
}
  
  
export async function getStaticProps({ params: { slug } }) {
    const res = await fetch(`${API_URL}events?slug=${slug}`)
    const events = await res.json()
  
    return {
      props: {
        evt: events[0],
      },
      revalidate: 1,
    }
}

I understand everyone is just as occupied as I am, so any assistance would be greatly appreciated.

Answer №1

The Strapi API has been updated to now return an object instead of an array. This new object contains a data array that can be easily looped through using the map() method, so remember to destructure it in your code:

 const { data } = await response.json();

Check out the API response structure here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Web app that utilizes preventDefault() to act independently - not compatible with image hyperlinks

I am working on a web application that I would like to appear as a standalone once a user has added it to the home screen of their device. I have been testing this functionality on an iPhone 5 and using meta tags to create a web app experience. Additionall ...

Guide to creating a vertical handler that can be resized

Did you know that you can resize tables in http://www.jsfiddle.net? I'm curious about how to resize just the "Vertical Handler". Could someone share the source code with me? If possible, please provide an example on http://www.jsfiddle.net. ...

handling a click event for an unresponsive ID (or so I assume)

After developing a pop-up button module using Bootstrap for integration into all product pages, I encountered a challenge that I need help with. The problem is outlined below. This is my first time posting here, so I'm open to feedback on the necessi ...

Spinning points about a center point

My goal is to rotate 4 points around a specific origin. Initially, the points appear in the correct position without any rotation. The code for this initial positioning is as follows: let origin = this.transform.position; for (let i in this._pointsOrig) { ...

Navigate to the following div, navigate back to the previous div

I am attempting to implement a div navigation system with next/previous buttons. Despite searching extensively on Google, I have not found the exact solution I am looking for. First and foremost, I want to maintain the integrity of my html structure. < ...

CORS response header is not included

Is it the company's responsibility () to rectify one of their endpoints or is it an issue with my code? I have tested all other endpoints () using the same code below and they all function correctly, except for the specific one in question. The erro ...

How to retrieve the values of a property from a JavaScript object

Looking for a fresh perspective on this issue. I'm currently retrieving historical data from the cryptocompare API, but I'm having trouble accessing the received values. https://i.sstatic.net/oH6hz.png When I hardcode it as res.data.OMG.USD, I ...

What are the steps for implementing dynamic head in Next.js 13?

In the era before Next JS 13, dynamic head content was imported. However, with the introduction of the head.js class in Next JS 13, changing the title and description in the head when loading a dynamic page became a challenge. Directly importing next/hea ...

Images within inline blocks appear to be extending beyond their containing div as though they are

When I check my website in various browsers such as Firefox, IE, Edge, and Safari, I noticed that the images in the "My Specialties" section of the left column are extending outside of the div instead of fitting within it. This problem also occurs in Chrom ...

The JSON data is being loaded twice through AJAX, when it should only load once upon clicking

Utilizing AJAX and jQuery, I am loading a JSON object onto my webpage with success. However, there is an issue where the data is being loaded twice about 50-70% of the time, resulting in the second set of data being appended to the page. Is there a method ...

Utilizing Vue.js: Dynamically linking v-model to route parameters depending on the current state

I'm currently in the process of developing an application that will serve as the backbone for a restaurant chain's website. The main functionality involves allowing users to modify page content and images. Given the complexity of the site with it ...

From JSON to JavaScript transformations

Currently, I am attempting to utilize JSON with data retrieved from the server by implementing this PHP script: include("db_connect.php"); mysql_connect($host,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $resu ...

Encountered a problem parsing JSON data that begins with "var ..."

While attempting to retrieve a JSON file from a server, I encountered an issue. The JSON file contains the following data: var reviews = [ { "firstName":"Simon", "lastName":"Lock", "fullName":"Simon Lock", "location":" ...

Retrieving data from a <span> element within a webpage created using ReactJS

I am completely new to the world of web design and development, so there may be some mistakes in my request. Essentially, I have a webpage that contains numerous span tags like the following example: These span tags are part of a significantly large DOM t ...

Tips for modifying animations based on screen width thresholds

Currently, I've implemented the data-aos attribute to add a "fade-up-right" animation to an element on my website, as shown below: <div className="des__Container" data-aos="fade-up-right"> However, I now want to modify this ...

Encountering a null pointer exception when launching the Chrome browser using Selenium

Hope you all are doing well. I need assistance in resolving a null pointer issue while developing a new Selenium framework for my company. The problem arises after calling the method "StartBrowser()" from the base class in the browser class. Everything ru ...

Enable a function to be triggered when clicking on an entire div

In my code, I have set an eventHandler on the document itself that targets a specific element with an id. This targeted element is a div containing some content. The eventHandler is triggered by a 'click' action on the document. However, I have e ...

HTTP Proxy: Discard partially completed requests

My Node.js-based HTTP Proxy has the ability to transform response body on the fly in a streaming fashion. Similar to nginx, my proxy compresses the response body using libz for gzipping. https://i.sstatic.net/VrgX7.jpg However, I am faced with a question: ...

Navigate through each file or image within a directory using Angular

I am facing a challenge with my app where each product has a gallery containing a random number of images, each with a completely unique name. These images are located in /src/assets/images/products/:id/. Currently, I am struggling to loop through and add ...

"Encountered a 'NextAuth expression cannot be called' error

Recently, I delved into learning about authentication in Next.js using next-auth. Following the documentation diligently, I ended up with my app/api/auth/[...nextauth]/route.ts code snippet below: import NextAuth, { type NextAuthOptions } from "next-a ...