Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error: Cannot read property 'map' of undefined (0:undefined) build on aws amplify

Tags:

reactjs

I am facing the following error while building on AWS amplify:

Syntax error: Cannot read property 'map' of undefined (0:undefined)

Here is my code:

import React from 'react';   
import ReactDOM from 'react-dom'; 
export default class BusinessHTTPService {
static getBusinessList = () => {
  
    return axios.get(`${API_BASE}business-categories/?`).then(response => response.data);
  };
  
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
like image 203
Harish Avatar asked Aug 29 '20 09:08

Harish


People also ask

How do you fix Typeerror Cannot read property map of undefined?

The "cannot read property 'map' of undefined" error occurs when we call the map() method on an undefined value, most often when the map method is called before the data from an API request has arrived. To solve the error, initialize the value you're mapping over to an empty array.

How do you fix Cannot read property of undefined In react?

The "Cannot read property 'props' of undefined" error occurs when a class method is called without having the correct context bound to the this keyword. To solve the error, define the class method as an arrow function or use the bind method in the classes' constructor method.

What does cannot read properties of undefined (reading 'MAP') Mean?

The "Uncaught TypeError: Cannot read properties of undefined (reading 'map')" error occurs in JavaScript, whenever you try to use the array map method on a variable that is undefined. This can usually happen in React, whenever you need to loop through an array of elements and display them.

Why is my map undefined in react?

This can usually happen in React, whenever you need to loop through an array of elements and display them. However, the array on which you are executing the map is undefined. This means that JavaScript sees the following code, and throws the above error:

Why can’t I call map on undefined?

As we know from the error message, it’s problematic to try to call map on undefined! This first quick fix might be enough for your use case: just default your stateful variable to an array while you’re waiting for your data to fetch.

Why does react render multiple renders when a variable is undefined?

The variable you are trying to map over is undefined. It will probably eventually be an array, but due to the asynchronous nature of React, you are experiencing at least one render when the variable is undefined. Let’s take this example code. In it, we fetch some data from an API and set state with that data.


1 Answers

I had the same issue with by build failing on AWS Amplify Console, but was fine locally.

How to reproduce locally

I was able to reproduce it locally by following these steps:

  1. run "npm ci"
  2. run "npm start"

"npm start" would fail, and show the error "Line 0: Parsing error: Cannot read property 'map' of undefined".

How to solve

I was able to solve it eventually by following these steps:

  1. downgrade typescript from version 4.0.2 to version 3.9.7.
  2. run "npm ci"
  3. run "npm start"

I don't see the error anymore, and when I push to AWS Amplify Console, it builds without issues.

like image 198
Enrico Pangan Avatar answered Oct 21 '22 14:10

Enrico Pangan