无法将符号值转换为字符串-React AJAX删除请求 [英] Cannot convert a Symbol value to a string - React AJAX delete request

查看:95
本文介绍了无法将符号值转换为字符串-React AJAX删除请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么这会在控制台中抛出Cannot convert a Symbol value to a string.我正在使用React v15和jQuery v3.

I can't figure out why this is throwing Cannot convert a Symbol value to a string in the console. I'm using React v15 and jQuery v3.

这是我的React代码:

Here's my React code:

var CommentList = React.createClass({
  handleDelete: function(comment) {
    console.log(comment);
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      type: 'DELETE',
      data: comment,
      contentType:'application/json',
      dataType: 'text',
      success: function(data) {
        this.setState({ data: data });
      }.bind(this),
      error: function(xhr, status, err) {
        this.setState({data: comments});
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    })
  },
  render: function() {
    var commentNodes = this.props.data.map(comment => {
      return(
        <div key= { comment.id }>
          <Comment author = { comment.author }>
            { comment.text }
          </Comment>
          <button onClick={this.handleDelete}>delete</button>
        </div>
      );
    });
    return (
      <div className="commentList">
        {commentNodes}
      </div>
    );
  }
});

json文件如下:

[
    {
        "id": 1388534400000,
        "author": "Pete Hunt",
        "text": "Hey there!"
    },
    {
        "id": 1323434400000,
        "author": "Ben Jerry",
        "text": "I did a thing"
    },
    ...
    ...
]

推荐答案

您没有将注释传递给handleDelete,它实际上是在接收click事件.您可能只使用注释ID就可以逃脱.尝试使用此列表渲染器:

You aren't passing the comment to handleDelete, it is actually receiving the click event instead. You could probably get away with just using the comment id. Try this for the list renderer:

<div key= {comment.id}>
  <Comment author ={comment.author}>
    {comment.text}
  </Comment>
  <button onClick={this.handleDelete.bind(this, comment)}>delete</button>
</div>

这篇关于无法将符号值转换为字符串-React AJAX删除请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆