Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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
Javascript ReactJS";未处理的拒绝(TypeError):this.state.features.map不是函数;_Javascript_Reactjs_Ecmascript 6_Fetch - Fatal编程技术网

Javascript ReactJS";未处理的拒绝(TypeError):this.state.features.map不是函数;

Javascript ReactJS";未处理的拒绝(TypeError):this.state.features.map不是函数;,javascript,reactjs,ecmascript-6,fetch,Javascript,Reactjs,Ecmascript 6,Fetch,我正在学习React,现在我正在尝试使用map执行get-resquest和list,但当我运行此代码时,他们会出现以下错误:“未处理的拒绝(TypeError):this.state.features.map不是函数”。我已经搜索过了,但我不知道这是怎么回事 import React, { Component } from 'react'; import './App.css'; class App extends Component { constructor() {

我正在学习React,现在我正在尝试使用map执行get-resquest和list,但当我运行此代码时,他们会出现以下错误:“未处理的拒绝(TypeError):this.state.features.map不是函数”。我已经搜索过了,但我不知道这是怎么回事

   import React, { Component } from 'react';
import './App.css';

class App extends Component {

  constructor() {
    super();
    this.state = {
      features: [{
        id: 1,
        name: 'Test',
        count: 1
      }]
    }
  }

  componentWillMount() {
    fetch("http://demo6085176.mockable.io/features")
      .then(response => response.json())
      .then(json => {
        console.log(json);
        this.setState({
          features: json,
        });
      });
    console.log(this.state.features)

  }

  render() {
    return (
      <div className="App">
        <ul>
          {
            this.state.features.map(function(feature){
              return (
                <li key={feature.id}><button type="button">Upvote</button> ({feature.count}) <span>{feature.name}</span></li>
              )
            })
          }
        </ul>
      </div>
    );
  }
}

export default App;
import React,{Component}来自'React';
导入“/App.css”;
类应用程序扩展组件{
构造函数(){
超级();
此.state={
特点:[{
id:1,
名称:'测试',
计数:1
}]
}
}
组件willmount(){
取回(“http://demo6085176.mockable.io/features")
.then(response=>response.json())
。然后(json=>{
log(json);
这是我的国家({
特点:json,
});
});
console.log(this.state.features)
}
render(){
返回(
    { this.state.features.map(函数(功能){ 返回(
  • Upvote({feature.count}){feature.name}
  • ) }) }
); } } 导出默认应用程序;
在您的组件willmount中,只需执行以下操作:

componentWillMount() {
   fetch("http://demo6085176.mockable.io/features")
    .then(response => response.json())
    .then(json => {
      console.log(json);
      this.setState({ features: json.features });
   });
}

您从API得到的响应是一个对象,该对象具有一个键
features
,它是您所需数据的对象数组。

在您的组件中,只需执行以下操作:

componentWillMount() {
   fetch("http://demo6085176.mockable.io/features")
    .then(response => response.json())
    .then(json => {
      console.log(json);
      this.setState({ features: json.features });
   });
}

您从API得到的响应是一个对象,该对象的键为
features
,它是您所需数据的对象数组。

console.log(json)是什么意思?“json”未定义您必须等待
fetch
完成。。。设置类似“加载…”的内容,在完成提取后,映射它。通过适当的调试,您的问题得到了解决。假设
console.log
时未定义
json
,那么服务器的输出可能有问题。@AndreLi不同意,他不需要等到fetch。console.log(json)是什么意思?“json”没有定义您必须等到
fetch
完成。。。设置类似“加载…”的内容,在完成提取后,映射它。通过适当的调试,您的问题得到了解决。假设
console.log
时未定义
json
,那么服务器的输出可能有问题。@AndreLi不同意,他不需要等到fetch。