简体   繁体   English

React.js:未被捕获的TypeError:undefined不是一个函数

[英]React.js : Uncaught TypeError: undefined is not a function

I'm trying to learn react.js and found a tutorial online. 我正在尝试学习react.js并在线找到了一个教程。 I was following a react/rails tutorial from http://rny.io/rails/react/2014/07/31/reactjs-and-rails.html 我正在关注来自http://rny.io/rails/react/2014/07/31/reactjs-and-rails.html的react / rails教程

The all the code was working fine until I got to the last step implementing the Comment Form. 所有代码都运行良好,直到我执行注释表单的最后一步。 After following all the instructions, I get an error in my chrome console pointing to this line of code var commentNodes = this.props.comments.map(function (comment, index) { saying its "Uncaught TypeError: undefined is not a function". The form shows up and accepts input but nothing is displayed after I submit. Also, the tutorial is a bit dated, it is still using React.renderComponent , I changed it to React.render after reading the docs. Is there some more deprecated code I missed? or Can anyone help me or tell me what I did wrong? Thanks in Advance 按照所有说明进行操作后,我在chrome控制台中遇到一个错误,指向此行代码var commentNodes = this.props.comments.map(function (comment, index) {表示其“未捕获的TypeError:未定义不是函数”表单显示并接受输入,但是提交后什么也没有显示。此外,该教程有点过时了,它仍然使用React.renderComponent ,阅读文档后我将其更改为React.render 。代码我错过了吗?或任何人都可以帮助我或告诉我我做错了什么吗?

var Comment = React.createClass({
      render: function () {
        return (
          <div className="comment">
            <h2 className="commentAuthor">
              {this.props.author}
            </h2>
              {this.props.comment}
          </div>
      );
  }
});

var CommentList = React.createClass({
  render: function () {
    var commentNodes = this.props.comments.map(function (comment, index) {
      return (
        <Comment author={comment.author} comment={comment.comment} key={index} />
        );
    });

    return (
      <div className="commentList">
        {commentNodes}
      </div>
      );
  }
});

var CommentBox = React.createClass({
  getInitialState: function () {
    return {comments: []};
  },
  componentDidMount: function () {
    this.loadCommentsFromServer();
  },
  loadCommentsFromServer: function () {
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      success: function (comments) {
        this.setState({comments: comments});
      }.bind(this),
      error: function (xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },
  handleCommentSubmit: function(comment) {
    var comments = this.state.comments;
    var newComments = comments.concat([comment]);
    this.setState({comments: newComments});
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      type: 'POST',
      data: {"comment": comment},
      success: function(data) {
        this.loadCommentsFromServer();
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },
  render: function () {
    return (
      <div className="commentBox">
        <h1>Comments</h1>
        <CommentList comments={this.state.comments} />
        <CommentForm onCommentSubmit={this.handleCommentSubmit}/>
      </div>
      );
  }
});

var CommentForm = React.createClass({
  handleSubmit: function() {
    var author = this.refs.author.getDOMNode().value.trim();
    var comment = this.refs.comment.getDOMNode().value.trim();
    this.props.onCommentSubmit({author: author, comment: comment});
    this.refs.author.getDOMNode().value = '';
    this.refs.comment.getDOMNode().value = '';
    return false;
  },
  render: function() {
    return (
      <form className="commentForm" onSubmit={this.handleSubmit}>
        <input type="text" placeholder="Your name" ref="author" />
        <input type="text" placeholder="Say something..." ref="comment" />
        <input type="submit" value="Post" />
      </form>
      );
  }
});

var ready = function () {
  React.render(
    <CommentBox url="/comments.json" />,
    document.getElementById('comments')
  );
};

$(document).ready(ready);

The likely reason is that the comments coming back is not an array. 可能的原因是返回的注释不是数组。 Do a console.log(comments) or debugger; 做一个console.log(comments)debugger; before this line: this.setState({comments: comments}); 在此行之前: this.setState({comments: comments}); to check your ajax response and see if comments is an array of comments. 检查您的ajax响应,并查看注释是否为注释数组。 If it's anything other than array, then that's your problem, you can just put some mock data in there for now until you can get that working. 如果不是数组,那是您的问题,您可以暂时将一些模拟数据放入其中,直到可以正常工作为止。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM