无法使用带有componentDidMount的axios渲染Redux中从MongoDB Atlas获取的数据

时间:2019-06-16 21:45:25

标签: mongodb react-redux axios

在通过redux从Mongodb Atlas提取文档(轴)时,我遇到了问题。我使用componentDidMount来解决获取和存储的异步时间,但由于“ this.props.cityList.map不是函数”,它在“ let listImage = this.props.cityList.map((i)...”处显示错误

我将“ let listImage = this.props.cityList.map((i)...”替换为“ console.log('city', this.props)”,它会在控制台中正确显示所有必需的数据

下面是代码:

import React, {Component} from 'react';
import { connect } from 'react-redux';
import {bindActionCreators} from 'redux';
import {getCity} from '../actions/cityRead';

class DisplayCities extends React.Component {
    componentDidMount() {
            this.props.getCity()        
    }
    cityLine() {
            //console.log('city', this.props)
            let listImage = this.props.cityList.map((i) =>{        
            return <img src={i.image} />;
            });
        return listImage;
        }     
    render() {
        return (
            <div style={{marginTop: 10}}>
                {this.cityLine()}
            </div>
        );
    }
}
const mapStateToProps = (state) => {
    return {
        cityList: state.cities
    }
}
const mapDispatchToProps = (dispatch) => { 
    return bindActionCreators({getCity: getCity}, dispatch)
}
export default connect(mapStateToProps,mapDispatchToProps)(DisplayCities)

预期结果将显示所有通过“ map()”的图像,但是会出现错误。非常感谢您的帮助,谢谢

0 个答案:

没有答案