2

I am new to react js. How to map through the data I am getting json response from ajax.I know i did wrong some place But i don't know where.This the error I am getting

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

enter image description here This is My code

/**
 * Created by arfo on 6/26/2016.
 */
var React =require('react');
var api = require('../utils');


var Bulkmail = React.createClass({
    getInitialState:function () {
      return{
          default:10,
          data:'',
          color:'#58FA58'

      }
    },
    componentDidMount:function () {
        api.getemail(this.state.default).then(function (response) {
           this.setState({
               data:response

           })
        }.bind(this))
    },
    onSubmit:function (e) {
      e.preventDefault();
      console.log(this.refs.text.value.trim());


    },

    onChange:function (e) {
     e.preventDefault();
        //console.log(this.refs.text.value.trim())
        var data = this.refs.text.value.trim();
        if(isNaN(data)){
            this.setState({
                color:'#FE2E2E'
            })
        }else{
            this.setState({
                color:'#58FA58'
            })
        }
    },
    render:function () {
        console.log(this.state.data);
        var results = this.state.data;
        return(
           <div className="bodybox">
               <div className="box">
                  <div className="upc">
                      <p>Generate Bulk Email</p>
                      <form onSubmit={this.onSubmit}>
                      <input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}}  ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
                       <button>Get Data</button>
                      </form>
                      <div className="result">
                          <ul>
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                              {this.state.data.map(function (data) {
                                  return  <li>data.email</li>
                              })}
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

                          </ul>
                      </div>

                  </div>
                   <div className="tdown">

                       <p>Json Format</p>


                       <textarea  defaultValue={"json"} />
                   </div>
               </div>
           </div>
        )
    }
});

module.exports = Bulkmail ;

3 Answers 3

3

set data:[ ] in

getInitialState:function () {
      return{
          default:10,
          data:[],
          color:'#58FA58'

      }
    },

and check

componentDidMount:function () {
    api.getemail(this.state.default).then(function (response) {

    console.log('CHECK',response) //   <----------------

       this.setState({
           data:response

       })
    }.bind(this))
},
Sign up to request clarification or add additional context in comments.

Comments

1
Add 

getInitialState: function() {
     return{
      default:10,
      data:[],
      color:'#58FA58'

  }
}

Comments

1

Your state data is not defined as an array. Correct it as:

getInitialState:function () {

  return{
      default:10,
      data: [],
      color:'#58FA58'

  }
}

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.