未捕获的类型错误:无法读取属性'作者'在尝试将编辑功能添加到注释框时,为undefined

Uncaught TypeError: Cannot read property 'author' of undefined when trying to add edit functionality to a comment box

本文关键字:功能 编辑 添加 undefined 注释 作者 类型 错误 属性 读取      更新时间:2023-09-26

这是我的React组件。我遗漏了一些(不重要的)部分。我正在尝试为注释添加编辑功能,但遇到了一个错误(在底部)。

export default class CommentBox extends React.Component {
  constructor() {
    super()
    this.state ={
      showComments: false,
      comments: [
        { id: uuid.v4(), author: 'Clu', body: 'Just say no to love!', avatarUrl: 'images/default-avatar.png' },
        { id: uuid.v4(), author: 'Anne Droid', body: 'I wanna know what love is...', avatarUrl: 'images/default-avatar.png' }
      ]
    }
  }
  render() {
    const comments = this._getComments() || [];
    let commentList;
    if (this.state.showComments) {
      commentList = <div className="comment-list">{comments}</div>
    }
    return(
      <div className="comment-box">
        <h3>COMMENTS</h3>
        {this._getPopularMessage(comments.length)}
        <h4 className="comment-count">{this._getCommentsTitle(comments.length)}</h4>
        <button className="comment-toggle" onClick={this._toggleShowComments.bind(this)}>{this._toggleCommentButton()}</button>
        <CommentForm addComment={this._addComment.bind(this)}/>
        {commentList}
      </div>
    );
  }
  _toggleShowComments(event) {
    event.preventDefault()
      this.setState({showComments: !this.state.showComments})
  }
  _addComment(author, body) {
    const comment = {
      id: uuid.v4(),
      author: author,
      body: body
    }
    this.setState({comments: this.state.comments.concat([comment])})
  }
  _getComments() {
    return this.state.comments.map((comment) => {
      return (<Comment
                 author={comment.author}
                 avatarUrl={comment.avatarUrl}
                 value ={comment.body}
                 editing={comment.editing}
                 onEditClick={this._activateEdit.bind(this, null, comment.id)}
                 key={comment.id}
                 onDelete = {this._deleteComment.bind(this, null, comment.id)}
                 onEdit={this._editComment.bind(this, null, comment.id)}/>)
    })
  }
  _activateEdit(event, key) {
    this.setState({comments: this.state.comments.map((comment) => {
      if (comment.id === key) {
        comment.editing = true
      }
      return comment
    })})
  }

我认为这就是错误所在。如果省略这一部分,代码将按预期运行。但是,当我在中添加此项时,会出现Uncaught TypeError: Cannot read property 'author' of undefined错误。我不太确定为什么

  _editComment(event, key, value) {
    this.setState({comments: this.state.comments.map((comment) => {
      if (comment.id === key) {
        comment.editing = false
        comment.value = {value}
      console.log('edited!')
      }
    })})
  }
}

这是供参考的注释组件:

export default class Comment extends React.Component {
    constructor() {
      super();
      this.state = {
        isAbusive: false
      };
    }
    render() {
      let commentBody;
      if (!this.state.isAbusive) {
        commentBody = <div className='comment-body'>
                        <Editable
                        editing={this.props.editing}
                        value={this.props.value}
                        onEdit={this.props.onEdit}/>
                      </div>
      } else {
        commentBody = <em>Content marked as abusive</em>;
      }
      return(
        <div className="comment">
          <p className="comment-header">{this.props.author}</p>
            {commentBody}
          <div className="comment-actions">
            <VotingButtons />
            <a className='comment-actions-edit' href="#" onClick={this.props.onEditClick}>Edit comment</a>
            <a className='comment-actions-delete' href="#" onClick={this.props.onDelete}>Delete comment</a>
            <a className='comment-actions-abuse' href="#" onClick={this._toggleAbuse.bind(this)}>Report as abuse</a>
          </div>
        </div>
      );
    }

我强烈建议不要更改this.state.comments(就像您在地图中所做的那样),而是返回一个带有更新值的新实例。

_editComment(event, key, value) {
    this.setState({
        comments: this.state.comments.map((comment) => {
            if (comment.id === key) {
                return {
                    ...comment,
                    editing: false,
                    value: value
                }
            } 
            return comment;
        })
    })
}

comment.value = value而非comment.value = {value}