2016-10-23 2 views
0

이유는 라이브 서버에이 오류가 발생합니까 :ReactJS - this.state. [object] .map은 함수가 아닙니다.

Uncaught TypeError: this.state.navitems.map is not a function

을하지만 내 로컬 호스트에서이 오류가 발생하지 않습니다.

reactjs :

import React from 'react'; import $ from 'jquery'; 

import NavItem from './nav-item'; 

class Footer extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = { 
      navitems: [], 
     }; 
    } 

    // Then fetch the data using $.get(): 
    componentDidMount() { 
     this.serverRequest = $.get(this.props.source, function (result) { 
      this.setState({ 
       navitems: result 
      }); 
     }.bind(this)); 
    } 

    componentWillUnmount() { 
     this.serverRequest.abort(); 
    } 

    render() { 
     var loop = this.state.navitems.map(function(item, index){ 
      return <NavItem key={index} item={item}></NavItem>; 
     }); 

     return (
      <div> 
       <div className="nav"> 
        <ul>{ loop }</ul> 
       </div> 
      </div> 
     ) 
    } } 

export { Footer as default } 

어떤 아이디어가 내가이 문제를 해결할 수있는 방법?

는 편집 :

componentDidMount() { 
    console.log('props.source', this.props.source); 
    this.serverRequest = $.get(this.props.source, function (result) { 
     console.log('returned result', result); 
     this.setState({ 
      navitems: result 
     }); 
    }.bind(this)); 
} 

결과 :

props.source ./data/nav.json 

returned result [{ 
    "navId": "1", 
    "title": "Home", 
    "code": "home", 
    "href": null, 
    "style": null, 
    "sort": "1", 
    "url": "#", 
    "parentId": null, 
    "totalChildren": "0", 
    "createdOn": null, 
    "updatedOn": null 
}, { 
    "navId": "2", 
    "title": "About", 
    "code": "about", 
    "href": null, 
    "style": null, 
    "sort": "2", 
    "url": "#", 
    "parentId": null, 
    "totalChildren": "0", 
    "createdOn": null, 
    "updatedOn": null 
}, { 
    "navId": "3", 
    "title": "Contact", 
    "code": "contact", 
    "href": null, 
    "style": null, 
    "sort": "3", 
    "url": "contact", 
    "parentId": null, 
    "totalChildren": "0", 
    "createdOn": null, 
    "updatedOn": null 
}] 

답변

0
아마 문제를 추측 할

는 여기있다 :

// Then fetch the data using $.get(): 
componentDidMount() { 
    this.serverRequest = $.get(this.props.source, function (result) { 
     this.setState({ 
      navitems: result 
     }); 
    }.bind(this)); 
} 

당신이 this.props.source이 동일 무엇을 검사 할 수 있었나요? 올 바르면 도메인 간 요청 문제가 있습니까? 일부 기록에 추가하고 관리자가 당신에게주는 무슨 참조 :

// Then fetch the data using $.get(): 
componentDidMount() { 
    console.log('props.source', this.props.source); 
    this.serverRequest = $.get(this.props.source, function (result) { 
     console.log('returned result', result); 
     this.setState({ 
      navitems: result 
     }); 
    }.bind(this)); 
} 
+0

감사합니다. 이 문제로 인해 문제가 해결 될 수 있습니까? – laukok

+0

도메인 간 요청 문제는 무엇입니까? – laukok

+1

도메인 간 "CORS"로 인해 문제가 발생하면 요청을 다시 보내는 서버에 올바른 헤더를 설정해야합니다. 요청에 대해'Access-Control-Allow-Origin : *'헤더를 추가하되 예기치 않은 요청까지 서버를 열지 않도록주의하십시오. – casr

0

내 빠른 해결책 - $.getJSON :

componentDidMount() { 
     this.serverRequest = $.getJSON(this.props.source, function (result) { 
      this.setState({ 
       article: result 
      }); 
     }.bind(this)); 
    } 
관련 문제