简体   繁体   English

reactjs Uncaught TypeError:无法读取未定义的属性'map'

[英]reactjs Uncaught TypeError: Cannot read property 'map' of undefined

I have used getInitialState on my BrowseWidgetBox component. 我在我的BrowseWidgetBox组件上使用了getInitialState Although when passing the data to my MainMenu component, the data remains empty, as if the AJAX call to the api was never run in the BrowseWidgetBox . 尽管将数据传递到MainMenu组件时,数据仍然为空,就好像从未在BrowseWidgetBox运行对api的AJAX调用BrowseWidgetBox

My question then, is why is this happening? 那么我的问题是,为什么会这样? Shouldn't componentDidMount call the ajax api and re-set the state to include the contents of the ajax call? componentDidMount不应该调用ajax api并重新设置状态以包含ajax调用的内容吗? I want the state of my groupsData and my itemData to be present when the page is initially loaded. 我希望在初始加载页面时显示我的groupsData和itemData的状态。 I am a bit worried that getInitialState is hindering the calls to ajax at least 'initially' which is causing my error. 我有点担心getInitialState至少在“最初”阻碍了对Ajax的调用,这导致了我的错误。

Here is the full code of the two components: 这是两个组件的完整代码:

var MainMenu = React.createClass({
                render: function() {
                    console.log(this.props.groupsData);   // console.log here 
                    var categories = this.props.groupsData.objects.map(function(obj){
                        return (<li>obj.description</li>);   
                    });
                    return (<div className="MainMenu">
                            <ul>{categories}</ul>

                        </div>);
                }
            });

var BrowseWidgetBox = React.createClass({
                getInitialState: function () {
                      return {groupsData: {}, itemsData: {}};
                },
                getGroupsApi: function(){
                    $.ajax({
                        url: this.props.groupsApi,
                        dataType: 'json',
                        type: 'GET',
                        success: function(groupsData){
                            this.setState({groupsData: groupsData});
                            console.log(groupsData)     // Console.log here 
                        }.bind(this),
                        error: function(xhr, status, err){
                            console.error(this.props.groupsApi ,status, err.toString());
                        }.bind(this)
                    });

                },
                getItemsApi: function() {
                 $.ajax({
                        url: this.props.itemsApi,
                        dataType: 'json',
                        type: 'GET',
                        success: function(itemsData){
                            this.setState({itemsData: itemsData});
                        }.bind(this),
                        error: function(xhr, status, err){
                            console.error(this.props.groupsApi ,status, err.toString());
                        }.bind(this)
                    });
                },
                componentDidMount: function() {
                    this.getGroupsApi();
                    this.getItemsApi();
                },
                render: function() {
                    return (<div className="BrowseWidgetBox">
                                <MainMenu groupsData={this.state.groupsData} itemsData={this.state.itemsData} />
                                <Display  />
                            </div>);
                }
            });



                React.render(
                    <BrowseWidgetBox groupsApi="http://this/is/a/good/url" itemsApi="http://this/is/a/good/api/call" />, document.getElementById('widget-container')
                );

You're trying to use the map in object... 您正在尝试在对象中使用地图...

In

 getInitialState: function () {
         return {groupsData: {}, itemsData: { objects: [] }};
 },

the first render are getting a object in groupsData try change to 第一个渲染在groupsData中获取对象尝试更改为

var MainMenu = React.createClass({
                render: function() {
                    console.log(this.props.groupsData);   // console.log here 
                    var categories = this.props.groupsData.objects.map(function(obj){
                        return (<li>obj.description</li>);   
                    });
                    return (<div className="MainMenu">
                            <ul>{categories}</ul>

                        </div>);
                }
            });

var BrowseWidgetBox = React.createClass({
                getInitialState: function () {
                      return {groupsData:  { objects: [] }, itemsData: []};
                },
                getGroupsApi: function(){
                    $.ajax({
                        url: this.props.groupsApi,
                        dataType: 'json',
                        type: 'GET',
                        success: function(groupsData){
                            this.setState({groupsData: groupsData});
                            console.log(groupsData)     // Console.log here 
                        }.bind(this),
                        error: function(xhr, status, err){
                            console.error(this.props.groupsApi ,status, err.toString());
                        }.bind(this)
                    });

                },
                getItemsApi: function() {
                 $.ajax({
                        url: this.props.itemsApi,
                        dataType: 'json',
                        type: 'GET',
                        success: function(itemsData){
                            this.setState({itemsData: itemsData});
                        }.bind(this),
                        error: function(xhr, status, err){
                            console.error(this.props.groupsApi ,status, err.toString());
                        }.bind(this)
                    });
                },
                componentDidMount: function() {
                    this.getGroupsApi();
                    this.getItemsApi();
                },
                render: function() {
                    return (<div className="BrowseWidgetBox">
                                <MainMenu groupsData={this.state.groupsData} itemsData={this.state.itemsData} />
                                <Display  />
                            </div>);
                }
            });



                React.render(
                    <BrowseWidgetBox groupsApi="http://this/is/a/good/url" itemsApi="http://this/is/a/good/api/call" />, document.getElementById('widget-container')
                );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 reactjs-未捕获的TypeError:无法读取未定义的属性&#39;map&#39; - reactjs - Uncaught TypeError: Cannot read property 'map' of undefined 未捕获到的TypeError:无法读取ReactJs中{Component} .render上未定义的属性“ map” - Uncaught TypeError: Cannot read property 'map' of undefined at {Component}.render in ReactJs React.js-未捕获的TypeError:无法读取未定义的属性&#39;then&#39; - Reactjs - Uncaught TypeError: Cannot read property 'then' of undefined 类型错误:无法读取未定义的属性“地图”| ReactJS - TypeError: Cannot read property 'map' of undefined | ReactJS ReactJS:TypeError:无法读取未定义的属性“map” - ReactJS: TypeError: Cannot read property 'map' of undefined TypeError:无法读取 reactJS 上未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined on reactJS Reactjs:类型错误:无法读取未定义的属性“地图” - Reactjs : TypeError: Cannot read property 'map' of undefined ReactJS TypeError:无法读取未定义的属性“ map” - ReactJS TypeError: Cannot read property 'map' of undefined TypeError:无法读取 ReactJs 中未定义的属性“映射”? - TypeError: Cannot read property 'map' of undefined in ReactJs? TypeError:无法读取未定义的属性“地图”(REACTJS) - TypeError: Cannot read property 'map' of undefined (REACTJS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM