简体   繁体   English

React Tutorial和Sinatra API:Uncaught TypeError:this.props.data.map不是函数

[英]React Tutorial and Sinatra API: Uncaught TypeError: this.props.data.map is not a function

I know, there are hundreds of questions with the same title, but nothing helped my get a solution for my problem. 我知道,有数百个问题具有相同的标题,但没有任何帮助我解决我的问题。 so I worked through the official react js tutorial and build a small API with sinatra to test things. 所以我通过官方的反应js教程,并用sinatra构建一个小API来测试。

so everything works really good. 所以一切都很好。 except of one error I see in the console when submitting a new "Joke" (called them jokes instead of comments ;)) via AJAX. 除了我在控制台中看到的一个错误,当提交一个新的“笑话”(称为笑话而不是评论;))通过AJAX。

app.js:66 Uncaught TypeError: this.props.data.map is not a function

This happens when I click on submit. 当我点击提交时会发生这种情况。 I logged the state when submitting the form and everything seems to be okay (array with the temporary objects). 我在提交表单时记录了状态,一切似乎都没问题(带有临时对象的数组)。

so the new Joke is being added and written to the database. 所以新的Joke被添加并写入数据库。 It works but i don't know why I'm getting the Uncaught TypeError in the console. 它工作但我不知道为什么我在控制台中得到未捕获的TypeError。

thanks in advance! 提前致谢!

var JokeBox = React.createClass({
  loadJokesFromServer: function() {
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      cache: false,
      success: function(data) {
        this.setState({data: data});
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },
  handleJokeSubmit: function(joke) {
    var jokes = this.state.data;
    var tmpJoke = jQuery.extend({}, joke)
    tmpJoke.id  = new Date();
    tmpJoke.likes = 0;
    jokes.unshift(tmpJoke);
    this.setState({data: jokes}, function(){
      $.ajax({
        url: this.props.url,
        dataType: 'json',
        type: 'POST',
        data: joke,
        success: function(data) {
          this.setState({data: data});
        }.bind(this),
        error: function(xhr, status, err) {
          this.setState({data: jokes});
          console.error(this.props.url, status, err.toString());
        }.bind(this)
      });
    });
  },
  getInitialState: function() {
    return {data: []};
  },
  componentDidMount: function() {
    this.loadJokesFromServer();
    setInterval(this.loadJokesFromServer, this.props.pollInterval);
  },
  render: function() {
    return (
      <div className="jokes">
        <h1>Jokes</h1>
        <JokeForm onJokeSubmit={this.handleJokeSubmit} />
        <JokeList data={this.state.data} />
      </div>
    );
  }
});

var JokeList = React.createClass({
  render: function() {
    var jokeNodes = this.props.data.map(function(joke) {
      return (
        <Joke content={joke.content} key={joke.id} likes={joke.likes} />
      );
    });
    return (
      <div className="jokeList">
        {jokeNodes}
      </div>
    );
  }
});

var JokeForm = React.createClass({
  getInitialState: function() {
    return {content: ''};
  },
  handleContentChange: function(e) {
    this.setState({content: e.target.value});
  },
  handleSubmit: function(e) {
    e.preventDefault();
    var content = this.state.content.trim();
    if (!content) {
      return;
    }
    this.props.onJokeSubmit({content: content});
    this.setState({content: ''});
  },
  render: function() {
    return (
      <form className="jokesForm" onSubmit={this.handleSubmit}>
        <input
          type="text"
          placeholder="Your Joke!"
          value={this.state.content}
          onChange={this.handleContentChange}
        />
        <input type="submit" value="Send joke" />
      </form>
    );
  }
});

var Joke = React.createClass({
  render: function() {
    return (
      <div className="joke">
        <p className="jokeContent">{this.props.content}</p>
        <p className="jokeLikes">{this.props.likes}</p>
      </div>
    );
  }
});

ReactDOM.render(
  <JokeBox url="/api/jokes" pollInterval={2000} />,
  document.getElementById('app')
);

// EDIT //编辑

So I played around with the sample tutorial repo from the tutorial. 所以我玩了教程中的示例教程repo。 I log the data in the handleSubmit in the success function right before the state is set. 在设置状态之前,我在成功函数中的handleSubmit中记录数据。 And I figured out: my data is ja object of the actual new Joke, in the sample tutorial it is an array of all comments. 我想通了:我的数据是实际新Joke的ja对象,在示例教程中它是所有注释的数组。 How could this be? 怎么会这样? I can't find my mistake... 我找不到我的错误......

try 尝试

handleJokeSubmit: function(joke) {

    let {data}= this.state;

    $.post(this.props.url, joke, res=>{
       this.setState({data:[res,...data]});
    })
   .fail( err=>alert('Error: ' + err.responseText));
}

///EDIT es5 ///编辑 es5

handleJokeSubmit: function(joke) {

    var data = this.state.data;

    $.post(this.props.url, joke, function(res){
       this.setState({data:[res].concat(data)});
    }.bind(this))
   .fail( function(err){alert('Error: ' + err.responseText)});
}

暂无
暂无

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

相关问题 未被捕获的TypeError:this.props.data.map不是一个函数-带ES6的React CommentBox教程 - Uncaught TypeError: this.props.data.map is not a function - React CommentBox Tutorial w/ ES6 React JS - 未捕获的类型错误:this.props.data.map 不是一个函数 - React JS - Uncaught TypeError: this.props.data.map is not a function 未捕获的TypeError:this.props.data.map不是函数 - Uncaught TypeError: this.props.data.map is not a function 未捕获的TypeError:this.props.data.map不是函数(使用超级代理) - Uncaught TypeError: this.props.data.map is not a function (using superagent) 抛出“TypeError:this.props.data.map不是函数”的反应代码 - React code throwing “TypeError: this.props.data.map is not a function” React,Array obj上的TypeError(this.props.data.map不是函数) - React, TypeError (this.props.data.map is not a function) on an Array obj × TypeError: this.props.data.map 不是 function - × TypeError: this.props.data.map is not a function Uncaught TypeError: _this.props.data.map is not a function, Map error to render Component - Uncaught TypeError: _this.props.data.map is not a function, Map error to render Component TypeError:this.props.data.map不是函数,但data是一个列表 - TypeError: this.props.data.map is not a function, but data is a list React Native:TypeError:undefined 不是一个对象(评估&#39;_this.props.data.map&#39;) - React Native: TypeError: undefined is not an object (evaluating '_this.props.data.map')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM