【问题标题】:React js map function error , data is getting set in state within ajaxReact js映射函数错误,数据在ajax中设置为状态
【发布时间】:2016-10-28 05:14:11
【问题描述】:

我作为数据得到的 ajax 响应如下:

{
 "totalCost": null,
  "listAmazonOutBean": [
    {
      "instanceId": "i-9820935f",
      "state": "stopped",
      "launchTime": "2016-02-08T14:46:18Z",
      "instanceType": "t2.micro",
      "placement": "us-west-2a",
      "listTags": [
        {
          "label": "Name",
          "value": "Micro RP test"
        },
        {
          "label": "Owner",
          "value": "JBU BBS"
        }
      ]
    },
    {
      "instanceId": "i-0b32c89f",
      "state": "stopped",
      "launchTime": "2016-07-22T21:06:38Z",
      "instanceType": "t1.micro",
      "placement": "us-west-2b",
      "listTags": [
        {
          "label": "aws:cloudformation:logical-id",
          "value": "LinuxEC2Instance"
        },
        {
          "label": "aws:cloudformation:stack-name",
          "value": "CodeDeploySampleStack-k0viff87wwolvvq2gldi"
        },
        {
          "label": "aws:cloudformation:stack-id",
          "value": "arn:aws:cloudformation:us-west-2:430671117617:stack/CodeDeploySampleStack-k0viff87wwolvvq2gldi/9c251e80-5021-11e6-a124-503f2a2cee82"
        },
        {
          "label": "Name",
          "value": "CodeDeployDemo"
        }
      ]
    },
    {
      "instanceId": "i-493dc7dd",
      "state": "stopped",
      "launchTime": "2016-07-22T21:06:38Z",
      "instanceType": "t1.micro",
      "placement": "us-west-2b",
      "listTags": [
        {
          "label": "Name",
          "value": "CodeDeployDemo"
        },
        {
          "label": "aws:cloudformation:logical-id",
          "value": "LinuxEC2Instance2"
        },
        {
          "label": "aws:cloudformation:stack-id",
          "value": "arn:aws:cloudformation:us-west-2:430671117617:stack/CodeDeploySampleStack-k0viff87wwolvvq2gldi/9c251e80-5021-11e6-a124-503f2a2cee82"
        },
        {
          "label": "aws:cloudformation:stack-name",
          "value": "CodeDeploySampleStack-k0viff87wwolvvq2gldi"
        }
      ]
    },
    {
      "instanceId": "i-3c33c9a8",
      "state": "stopped",
      "launchTime": "2016-07-22T21:06:39Z",
      "instanceType": "t1.micro",
      "placement": "us-west-2b",
      "listTags": [
        {
          "label": "aws:cloudformation:stack-id",
          "value": "arn:aws:cloudformation:us-west-2:430671117617:stack/CodeDeploySampleStack-k0viff87wwolvvq2gldi/9c251e80-5021-11e6-a124-503f2a2cee82"
        },
        {
          "label": "aws:cloudformation:logical-id",
          "value": "LinuxEC2Instance3"
        },
        {
          "label": "aws:cloudformation:stack-name",
          "value": "CodeDeploySampleStack-k0viff87wwolvvq2gldi"
        },
        {
          "label": "Name",
          "value": "CodeDeployDemo"
        }
      ]
    }
  ]
}

反应组件

var AwsInfo= React.createClass({

    getInitialState: function() {
                    return{

                        data: {},
                        response: undefined
    }},



    searchawsinfo : function(){
        var awsAccessKey = this.refs.awsAccessKey.value;
        var awsSecretKey = this.refs.awsSecretKey.value;
        var region = this.refs.region.value;
        var secure=JSON.parse(sessionStorage.getItem('globals'));
        securityToken=secure.currentUser.authToken;
        console.log(securityToken);
        console.log(region);
        var amazonInbean = {
            "awsAccessKeyId" : awsAccessKey,
            "awsSecretAccessKey" : awsSecretKey,
            "region" : region,
            "securityToken" :securityToken
        };
        console.log(amazonInbean);
        var self = this;
        self.setState({response: undefined});
        $.ajax({
                    type: "POST",
                        url: "http://localhost:9100/restfulws/rest/amazonmonitoring/getAWSInfo",
                        data: JSON.stringify(amazonInbean),
                        contentType: "application/json",
                        dataType: 'json',
                        success: function(data) { 

                                        console.log(data);
                                        self.setState({data: data});
                                        self.setState({response: data});
                                        console.log(self.state.data.listAmazonOutBean);
                                        console.log(self.state.response.listAmazonOutBean);
                                        console.log(self.state.response.totalCost);

                                }


                });


    },

    render : function(){
        var searchItems = this.props.response.map(function(search) {
                        return (<div>search.listAmazonOutBean
                                    </div>
                                );
                        }
                        );


    return(<div>{searchItems }</div> );

【问题讨论】:

  • 您可能想使用this.state.response 而不是this.props.response ?

标签: javascript ajax reactjs state repeat


【解决方案1】:

您可能遇到的错误是this.props.response is undefined。这是因为您没有将任何道具作为对组件的响应传递,而是在获取数据时设置了state response

所以你需要映射 state 而不是 prop as

render : function(){
    var searchItems = this.state.response.map(function(search) {
                    return (<div>search.listAmazonOutBean
                                </div>
                            );
                    }
                    );


return(<div>{searchItems }</div> );

您还应该在对响应运行映射之前检查响应是否未定义

var AwsInfo= React.createClass({

getInitialState: function() {
                return{

                    data: {},
                    response: undefined
}},



searchawsinfo : function(){
    var awsAccessKey = this.refs.awsAccessKey.value;
    var awsSecretKey = this.refs.awsSecretKey.value;
    var region = this.refs.region.value;
    var secure=JSON.parse(sessionStorage.getItem('globals'));
    securityToken=secure.currentUser.authToken;
    console.log(securityToken);
    console.log(region);
    var amazonInbean = {
        "awsAccessKeyId" : awsAccessKey,
        "awsSecretAccessKey" : awsSecretKey,
        "region" : region,
        "securityToken" :securityToken
    };
    console.log(amazonInbean);
    var self = this;
    self.setState({response: undefined});
    $.ajax({
                type: "POST",
                    url: "http://localhost:9100/restfulws/rest/amazonmonitoring/getAWSInfo",
                    data: JSON.stringify(amazonInbean),
                    contentType: "application/json",
                    dataType: 'json',
                    success: function(data) { 

                                    console.log(data);
                                    self.setState({data: data});
                                    self.setState({response: data});
                                    console.log(self.state.data.listAmazonOutBean);
                                    console.log(self.state.response.listAmazonOutBean);
                                    console.log(self.state.response.totalCost);

                            }


            });


},

render : function(){

    var searchItems = null;
    if(this.state.response != undefined) {
       searchItems = this.props.response.map(function(search) {
                    return (<div>search.listAmazonOutBean
                                </div>
                            );
                    }
                    );
    }


return(<div>{searchItems }</div> );

【讨论】:

  • 是的,即使是道具也可以。但我无法将响应数据设置为道具。我的意思是我不知道这样做,我是新来的反应。你能帮我解决一下吗
  • this.state.response.map 不是函数我现在收到此错误
  • props 是将从其父组件传递给组件的东西,因此您不能将其作为同一组件中的道具。另外你在哪里打电话searchawsinfo 和一些更多的事情,因为最初的响应被设置为未定义你会得到这个错误。将其更改为response: []
  • 非常感谢shubham。
猜你喜欢
  • 2021-07-17
  • 2019-05-05
  • 2022-06-27
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多