0

So I try to build some React Component on ES6 standars but my code breaks on server data fetching I keep getting Uncaught TypeError: Cannot read property 'map' of undefined

import React from 'react';

class PropertiesList extends React.Component {

  constructor(props){

    super(props);
    this.state = { data:[]}
  }


  loadPropertiesFromServer() {
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      success: (data) => {
        console.log(data);
        this.setState({data: data});
      },
      error: (xhr, status, err) => {
        console.error(this.props.url, status, err.toString());
      }
    });
  }

  componentDidMount() {
    this.loadPropertiesFromServer();
    setInterval(this.loadPropertiesFromServer.bind(this), this.props.pollInterval);;
  }

  render() {
    var propertyNodes = this.props.data.map((property) => {

    return (<Property>
                  {property.description}
            </Property>)
     });

    return <div className="row">
        <div className="col-lg-12">
          <h1>Available Properties</h1>
          {propertyNodes}
        </div>
    </div>;
  }
}

export default PropertiesList

Entry Point

import React from 'react';
import PropertiesList from './../components/propertylist.jsx';
import Filters from './../components/filter.jsx';
import Properties from './../models/PropertiesModel.js';
    class TransProperties extends React.Component {

      render() {

        return <div className="row">
            <div className="col-lg-9">
                <PropertiesList url="/properties.json"/>
            </div>
            <div className="col-lg-3">
                <Filters/>
            </div>
        </div>;
      }
    }

    export default TransProperties
6
  • How are you using this code? You can't "export default" 2 times in the same module Commented Jul 19, 2015 at 9:37
  • those are 2 different files Commented Jul 19, 2015 at 9:38
  • I edited your question to show it's 2 different files, but it looks incomplete (isn't the 2nd file missing import react...?). Is there anything missing from the code? Commented Jul 19, 2015 at 9:41
  • The data returned by your server doesn't seem to be an array, therefore it does not have a map method. What does the console.log call report? Commented Jul 19, 2015 at 10:00
  • Also: you should really clear the interval in componentWillUmnount Commented Jul 19, 2015 at 10:01

1 Answer 1

1

In your render() method, you try to access this.props.data rather than this.state.data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.