Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs TypeError:this.state.datacharts.map不是函数_Reactjs - Fatal编程技术网

Reactjs TypeError:this.state.datacharts.map不是函数

Reactjs TypeError:this.state.datacharts.map不是函数,reactjs,Reactjs,请帮忙,我的代码有问题,我见过一些类似的问题,但没有找到为什么会发生这种情况。我犯了个错误。TypeError:this.state.datacharts.map不是函数。。请帮我找出错误的地方,因为我刚刚 这是我的代码: import React, { Component } from 'react'; import ReactSpeedometer from "react-d3-speedometer" // Resolves charts dependancy const API = '

请帮忙,我的代码有问题,我见过一些类似的问题,但没有找到为什么会发生这种情况。我犯了个错误。TypeError:this.state.datacharts.map不是函数。。请帮我找出错误的地方,因为我刚刚 这是我的代码:

import React, { Component } from 'react';
import ReactSpeedometer from "react-d3-speedometer"

// Resolves charts dependancy
const API = 'http://localhost:9000/Sales_vs_Target';

class Chart_SalesVsTarget extends Component {
  constructor(props) {
    super(props);
    this.state = { 
      datacharts: '' 
    };
}

callAPI() {
  fetch(API)
      .then(res => res.text())
      // .then(res => this.setState({res}, () => 
      //   console.log('Datacharts fetched...', res)))
      .then(res => this.setState({ datacharts: res }));
}
componentDidMount() {
  this.callAPI();
}

  render() {
    return (
      <div>
        <center><h4>Sales vs Target</h4></center>
      <a href='/Table_nas'>
             <center>
               <div>
             {/* {this.props.apiResponse.map(apiResponses => */}
             <div style={{
                width: '500px',
                height: '300px',
                background: '#e4e5e6'
              }}>
              {this.state.datacharts.map(apiResponses => 
                <ReactSpeedometer
                  fluidWidth
                  minValue={0}
                  maxValue={100}
                  value={apiResponses.PERSEN_NAS}
                />
                )}
              </div>

               </div>
             </center>
         </a>
         </div>
    );
  }
}

export default Chart_SalesVsTarget;
import React,{Component}来自'React';
从“react-d3-车速表”导入react车速表
//解决图表的依赖性
常数API=http://localhost:9000/Sales_vs_Target';
类图表\u SalesVsTarget扩展组件{
建造师(道具){
超级(道具);
this.state={
数据图表:“”
};
}
callAPI(){
获取(API)
。然后(res=>res.text())
//.then(res=>this.setState({res},()=>
//log('Datacharts fetched…',res)))
.then(res=>this.setState({datacharts:res}));
}
componentDidMount(){
这是callAPI();
}
render(){
返回(
销售与目标
);
}
}
导出默认图表\u salesvtarget;

在调用ComponentDidMount之前,渲染函数将使用初始道具和状态运行。 您确实将初始状态设置为
datacharts:“
,这是一个字符串。但是String没有方法
String.map()
,所以它会出错


我建议在构造函数内部使用
this.state={datacharts:[]}

在调用ComponentDidMount之前,渲染函数将使用初始道具和状态运行。 您确实将初始状态设置为
datacharts:“
,这是一个字符串。但是String没有方法
String.map()
,所以它会出错


我建议在构造函数中使用
this.state={datacharts:[]}

您是否在控制台中记录了
this.state.datacharts
以查看它是否是一个数组,是否具有您首先需要的值?嗯,不清楚您的响应数据是字符串还是数组。从外观上看,您使用res.text()将数据转换为字符串。因此,即使使用它来更新状态,也不能使用.map(),因为它是一个数组方法。为了更好地帮助您,我们需要知道“res”是数组、对象、字符串还是其他。您是否记录了
this.state.datacharts
,以查看它是否是数组并具有您首先需要的值?嗯,不清楚您的响应数据是字符串还是数组。从外观上看,您使用res.text()将数据转换为字符串。因此,即使使用它来更新状态,也不能使用.map(),因为它是一个数组方法。为了更好地帮助您,我们需要知道“res”是否是数组、对象、字符串等。