【问题标题】:Angular ngx-chart: Object not iterableAngular ngx-chart:对象不可迭代
【发布时间】:2020-04-06 21:44:57
【问题描述】:

我对 Anguler ngx-charts 有疑问。我正在尝试绘制数据但仍然出现错误:
ERROR TypeError: data is not iterable.

如果我使用 ts 文件中定义的这些数据,绘图显示正确。

public data = [
  {
    "name": "Požadovaný prùtok vzduchu",
    "series": [
      {
        "value": "  923",
        "name": "0"
      },
      {
        "value": "  923",
        "name": "0"
      },
      {
        "value": "  924",
        "name": "1"
      },
      {
        "value": "  916",
        "name": "5"
      },
      {
        "value": "  916",
        "name": "8"
      }
    ]
  }
];

但我需要从 https 请求中绘制数据。我的代码是:

this.loadedFile.getContent(email,this.nazev).subscribe(res => { 
        console.log(this.data);
        this.data = res.data;
  });
getContent(folder:any, filename:any) {
    return this.httpClient.post<any>(apiUrl + 'loginApi/contentApi/file.php', {folder,filename});  
    }

结果 res.data 应该是格式正确的 JSON。

"data": {
        "name": "Požadovaný prùtok vzduchu",
        "series": [
            {
                "value": "  923",
                "name": "0"
            },
            {
                "value": "  923",
                "name": "0"
            },
etc...

这是我的代码,由于可迭代错误,我无法从请求中绘制数据。我尝试将其转换为字符串并返回 JSON 格式,用作字符串,创建数组,但没有任何效果。

谢谢! :)

【问题讨论】:

    标签: angular ngx-charts


    【解决方案1】:

    使用JSON.stringify()replace(){ 替换为[{,将} 替换为}] 和JSON.parse() 再次生成对象。

    let data = {
      "name": "Požadovaný prùtok vzduchu",
      "series": [{  "value": "  923",  "name": "0"},{  "value": "  923",  "name": "0"},{  "value": "  924",  "name": "1"},{  "value": "  916",  "name": "5"},{  "value": "  916",  "name": "8"}
      ]
    };
    console.log(JSON.parse(JSON.stringify(data).replace(/^\{(.*)\}$/,"[ { $1 }]")));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-31
      • 2021-07-31
      • 2019-11-12
      • 2017-01-05
      • 2015-11-11
      • 2021-06-06
      • 2013-05-27
      • 2013-08-02
      相关资源
      最近更新 更多