Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
is map function is not working or fetched data have some problem

getting error (Cannot read properties of undefined (reading 'map')) while rendering list. the code is attached below please help. REACT Code in which Data have been fetched and tried rendered.

import React, { Component } from 'react'
// import axios from 'axios';
export default class List extends Component {
  constructor(props)
  {
    super(props);
    this.state={apiResponse:[]};
    
  }
callAPI()
  {
    fetch("http://localhost:9000/testAPI")  
    .then( (res) => res.json()) 
    .then( (data) => {this.setState({apiResponse: data.task});});
  }

  componentWillMount()
  {
    this.callAPI();
  }

  render() {
    return (
      <div>
          
        <h1>{this.state.apiResponse}</h1>
        {
            this.state.apiResponse.map((r)=>
                <li >r.task</li>
            )
        }
      </div>
    )
  }
}

NODE JS from where the data is being fetched to react

router.get("/",function(req,res)
{

MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("to-do");
    // var query = { address: "Park Lane 38" };
        dbo.collection("to-do").find({}).toArray(function(err, result) {
            if (err) throw err;
            console.log(result);
            res.json(result)
            // res.send((result))
            db.close();
        });
    });

   
})  
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your problem is that your json response is not including the task-property (at least not every time). By setting apiResponse to this value you are setting it to undefined and the map-function is not available anymore. A workaround could be to check if the property task is available before setting the state.

fetch("http://localhost:9000/testAPI")  
    .then( (res) => res.json()) 
    .then( (data) => {
       if (data.task) {
          this.setState({apiResponse: data.task});
       }
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!