Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 为什么我会得到一个“;TypeError:this.state.users.map不是函数”;用于部署的React应用程序生产版本_Javascript_Arrays_Reactjs_Google Cloud Firestore_Axios - Fatal编程技术网

Javascript 为什么我会得到一个“;TypeError:this.state.users.map不是函数”;用于部署的React应用程序生产版本

Javascript 为什么我会得到一个“;TypeError:this.state.users.map不是函数”;用于部署的React应用程序生产版本,javascript,arrays,reactjs,google-cloud-firestore,axios,Javascript,Arrays,Reactjs,Google Cloud Firestore,Axios,使用React构建web应用程序,从Firestore数据库中提取用户信息。当在本地运行应用程序时,它运行得完美无缺,我可以加载用户信息。部署后,我现在收到一条“TypeError:this.state.users.map不是函数”消息,页面甚至不会加载。为什么在部署它之前它会工作?解决方案是什么 import React, { Component } from 'react'; import AllCoaches from '../components/search/AllCoaches';

使用React构建web应用程序,从Firestore数据库中提取用户信息。当在本地运行应用程序时,它运行得完美无缺,我可以加载用户信息。部署后,我现在收到一条“TypeError:this.state.users.map不是函数”消息,页面甚至不会加载。为什么在部署它之前它会工作?解决方案是什么

import React, { Component } from 'react';
import AllCoaches from '../components/search/AllCoaches';
import Banner from '../components/layout/banner';
import axios from 'axios';

// MUI Stuff
import Grid from '@material-ui/core/Grid';
import CircularProgress from '@material-ui/core/CircularProgress';


class coaches extends Component {
    state = {
        users: []
      }

  componentDidMount() {
    axios.get('/coaches')
        .then(res => {
            console.log(res.data)
            this.setState({
                users: res.data
            })
        })
        .catch(err => console.log(err));
  }

    render() {
    let coaches = this.state.users ? (
        this.state.users.map((user) => <AllCoaches user={user}/>)
    ) : ( <CircularProgress/> );
      return (
        <Grid container>
        <Banner/>
            {coaches}
        </Grid>
        );  
    }   
}


export default coaches

这里有很多问题,我要指出两个最明显的问题:

类名“coach”的第一个字母必须大写,才能成为有效的React组件并呈现给DOM

国家首先应定义如下:

constructor(props) {
    super(props);

    this.state = {
        users: [],
    };
}

一旦您应用了这些修复,您的代码应该可以正常工作。如果你现在或将来遇到任何问题,请随时发表评论,我会帮你解决

1.将组件名称大写

2.要使用state,必须调用super()方法:


因此,后端没有返回您认为应该返回的内容。因此,您的路由不起作用。我应用了上述修复程序,但仍收到相同的错误:“TypeError:this.state.users.map不是函数”我应用了上述修复程序,但仍收到相同的错误:“TypeError:this.state.users.map不是函数”console.log(this.state.users)在render and share resultResult中,console.log(this.state.user)是部署版本的数组(0)。本地主机的结果是(5)[{…},{…},{…},{…},{…},{…}]您在您的状态中定义了用户,但是您在console.log(this.state.user)和get空数组中查看您的state对象中的用户名
constructor(props) {
    super(props);

    this.state = {
        users: [],
    };
}
constructor(props) {
super(props);

this.state = {
    users: [],
   };
}