【问题标题】:JSON encode, parsing and calling confusionJSON编码、解析和调用混淆
【发布时间】:2016-06-27 05:22:10
【问题描述】:

所以我的服务器向客户端发送一个 json 编码的数组。 客户端在上面使用 JSON.parse,但是当我尝试提取数据时出现错误:TypeError: Cannot read property 'description' of undefined,但我确定描述是关键之一,因为运行:

和reactjs有关系吗?我的代码:

<script type="text/jsx">
/** @jsx React.DOM */
var HabitModel = React.createClass({
getInitialState: function(){
    return {
    data: [],
    pageAmount: 0
  };
},

componentDidMount: function(){
    this.serverRequest = $.get(this.props.source, {userid:123},      function(result){
    result = JSON.parse(result);
    this.setState({
      data: result,
      pageAmount: Math.ceil(result.length/4)
    });

  }.bind(this));
},

render: function() {
  return (
    <div>
      the habit is:
      <HabitList data={this.state.data} />
    </div>
  );
}
})

    var HabitList = React.createClass(
  {
    render: function(){
  return(
  { //error line here
    JSON.stringify(this.props.data[0].description)
   }
      )

}

  });

    React.render(
  <HabitModel source = "/health1/server/habit/user"/>,
  document.getElementById('myDiv')
    );
    </script>

错误在JSON.stringify(this.props.data[0].description)这一行 如果我输入this.props.data[0],它会完美运行,它会输出如下 JSON:

{"habitid":"1","userid":"123","description":"go to the gym","startDate":"0000-00-00","goalDate":"0000-00-00"}

如您所见,其中一个关键是描述。那么this.props.data[0].description 怎么不工作了呢?


编辑: 我发现如果我要在 ajax 调用之后立即执行 console.log

componentDidMount: function(){
this.serverRequest = $.get(this.props.source, {userid:123},      function(result){
result = JSON.parse(result);

console.log(result[0].description); //ADDED LINE, this would work

this.setState({
  data: result,
  pageAmount: Math.ceil(result.length/4)
});

  }.bind(this));
},

这将认识到描述是关键之一。 reactjs 是怎么回事?

【问题讨论】:

  • this.props 或 this.props.description[0] 似乎没有描述属性。使用console.log正确调试...
  • this.props.description的实际形状是什么?这个错误意味着this.props.description[0] 是undefined。
  • this.props.description[0] 很好,因为我可以使用 JSON.stringify 从中读取,正如您在帖子中看到的那样。 this.props.description 的实际形状是 [{"habitid":"1","userid":"123","description":"go to the gym","startDate":"0000-00-00" ,"goalDate":"0000-00-00"},{"habitid":"2","userid":"123","description":"努力学习","startDate":"0000-00-00 ","goalDate":"0000-00-00"},{"habitid":"3","userid":"123","description":"去公园","startDate":"0000- 00-00","goalDate":"0000-00-00"} [...] ]
  • 我明白为什么会出现混淆,这是我的变量命名。我将更改变量以使其更易于理解
  • @carlosDelgado,虽然它确实有 description 属性,这就是为什么我在帖子中展示了当我做console.log JSON.stringify(this.props.data[0])时得到的输出

标签: javascript json reactjs


【解决方案1】:

所以我发现了问题所在。 当您意识到 componentDidMount 在初始渲染发生之前不会运行时会变得很清楚。所以一开始它向孩子传递了一个空数据。这就是为什么我无法从数据中获取描述,尽管 JSON 中有描述键

【讨论】:

    【解决方案2】:

    在 JS 中,您可以使用方括号 [] 访问已解析 JSON 字符串的 JSON 属性。

    例如: 如果你已经在var1 中解析了你的json,并且你想从json 中获取“property”键,你应该使用var1["property"]。当然假设“属性”键是一级!

    【讨论】:

    • 是的,我知道这一点,并尝试过 JSON.stringify(this.props.description[0]["description"]),但我仍然遇到同样的错误
    【解决方案3】:

    这里没有足够的代码来查看为什么description[0] 的值是undefined 并且没有description 属性。

    创建HabitList时是否设置了属性?

    <HabitList description=[{description: "Aaa"}]/>
    

    尝试运行这样的代码,看看发生了什么

    var HabitList = React.createClass(
      {
        render: function () {
          return (
          <ul>
            {JSON.stringify(this.props.description)}
          </ul>
          )
        }
      })
    

    【讨论】:

    • 我已经尝试过运行该代码,它只给了我以下信息:[{"habitid":"1","userid":"123","description":"go to the gym ","startDate":"0000-00-00","goalDate":"0000-00-00"},{"habitid":"2","userid":"‌​123","description": "努力学习","startDate":"0000-00-00","goalDate":"0000-00-00"},{"habitid":"3","userid":‌​"123"," description":"去公园","startDate":"0000-00-00","goalDate":"0000-00-00"} [...] ]
    • 嗯,奇怪,你的代码应该可以工作...你能粘贴完整的错误,如果还有更多,比如堆栈跟踪。
    • 好的,我会将它与完整代码一起粘贴到原始帖子中。谢谢
    猜你喜欢
    • 2023-03-18
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多