vendredi 1 juillet 2016

Uncaught TypeError: Cannot read property 'then' of undefined in React ajax Call?


I am new to react js.I am little confused with loading initial data in reactjs. I am pretty sure that my ajax call is working But I don't Know How to handle that data and manipulate that json data to my component. App.js

var React = require('react');
var Actions = require('../actions');
var Store = require('../stores/store');
var Nav =require('./Nav');
var Fakeprofile = require('./Fakeprofile');
var Sidemenu = require('./Sidemenu');
var Bulkmail = require('./Bulkmail');
var store = require('../stores/store');
var api = require('../utils');
function getAppState() {

    return {

    }
}


var App = React.createClass({
   getInitialState:function () {
       return getAppState();
   },
    componentDidMount: function(){
        api.getprofile().then(function(response) {
            console.log(response);
            this.setState({
                data:response

            });
        });

        Store.addChangeListener(this._onChange);
    },

    componentUnmount: function(){
        Store.removeChangeListener(this._onChange);
    },



    render:function () {
        console.log(this.state.data);
        return(
            <div>
                <Nav/>
                <Sidemenu/>
                <Fakeprofile data={this.state.data} />



            </div>

        )
    },
    _onChange: function(){
        this.setState(getAppState());
    }
});

module.exports = App;

Utils.js

var actions = require('./actions');

module.exports = {
  getprofile:function () {
      console.log('Gettinf data');
      var url  = 'http://localhost:3000/api/index';
      $.ajax({
          url:url,
          dataType:'json',
          cache:false,
          success:function success(data) {
              console.log(data);



          }.bind(this),
          error:function error(xhr,status,err) {
              console.log(err);
          }
      })
  }
};

Aucun commentaire:

Enregistrer un commentaire