在地图中调用api

时间:2017-11-14 16:37:50

标签: javascript reactjs redux react-redux axios

我有两个使用redux编写的API和reducer。第一个api根据来自第一个api的用户ID获取用户列表和第二个api。

API如下 action.js

export const GET_EXPERT_LIST='GET_EXPERT_LIST';
export const GET_PROFILE_PHOTO='GET_PROFILE_PHOTO';
export const getExpertList=()=>{
    return(dispatch)=>{
        axios({
            method:'POST',
            url:Constants.URLConst+"/xyz",
            headers:Constants.headers,
            data:{
                "PageNum" : 1,
            }
        }).then((response)=>{
            return dispatch({
                type:GET_EXPERT_LIST,
                response
            })

        }).catch((error)=>{
            console.log(error);
        })
    }
}

export const getProfile=(userid)=>{
    return(dispatch)=>{
        axios({
             method:'GET',
             url:Constants.URLConst+"/UserProfileImage?enterpriseId="+userid,
             headers:Constants.headers
        }).then((response)=>{
             dispatch({
                type:GET_PROFILE_PHOTO,
                response
             })
        }).catch((error)=>{
            console.log(error);
        })
    }
}

在reducer.js中,我有 -

import {combineReducers} from 'redux';
import {ASK_EXPERT_DATA,GET_EXPERT_LIST,GET_PROFILE_PHOTO} from '../actions/ProfilePageActions.js';

export function getExpertList(state={},action){
    switch(action.type){
        case GET_EXPERT_LIST:
            return {...state, ...action.response}
            default:
                return state
    }
} 

export function getProfile(state={},action){
    switch(action.type){
        case GET_PROFILE_PHOTO:
            return {...state, ...action.response}
            default:
                return state
    }
}

const data=combineReducers({
    getExpertList,
    getProfile
})

export default data;

在主要的javascript文件中,我调用componentDidMount中的第一个api, 因为第一个api返回一个数组,我使用map()进行显示。

import React,{Component} from 'react';
import ReactDom from 'react-dom';
import {getExpert,getExpertList} from './actions/ProfilePageActions.js';
import {connect} from 'react-redux';
import theme from './assets/react-toolbox/theme';
import ThemeProvider from 'react-toolbox/lib/ThemeProvider';
import Card from 'react-toolbox/lib/card/Card.js';
import CardTitle from 'react-toolbox/lib/card/CardTitle';
import CardMedia from 'react-toolbox/lib/card/CardMedia';
import Chip from 'react-toolbox/lib/chip/Chip.js';
import CardText from 'react-toolbox/lib/card/CardText';

class FindExpert extends Component{

    state={
        countries: ['ES-es', 'TH-th'],
        source:[],
        valueSelected:[],
        experts:[]
    }   

    componentDidMount(){
        this.props.getExpertList();
    }

    componentWillReceiveProps(nextProps){

        if(nextProps.get_Expert_List && nextProps.get_Expert_List.data){
            this.setState({
                experts:[...nextProps.get_Expert_List.data.Experts] 
            })
        }
    }

    getExpertInfo(){
        var i=1;
        if (this.state.experts && this.state.experts.length) {
            this.test = this.state.experts.map((expertUSer)=> {
                this.props.getProfile(expertUSer.UserId);
                console.log(this.props.get_profile);
                return(
                    <ThemeProvider theme={theme} key={i++}>
                        <Card className='experts'>

                            <CardTitle title={expertUSer.Name}
                                subtitle={expertUSer.Designation}/>
                            <CardText>{expertUSer.Country}</CardText>
                        </Card>
                    </ThemeProvider>
                    )
            });

            return this.test;
        }
        return null;
    }

    render(){
        if(this.props.ask_expert.data){
            return(
                <div className='expert-div' id='expert-div'>{this.getExpertInfo()}</div> 
                    )
        }else{
            return null;
        }
    }
}

const mapStateToProps=(state)=>{
    console.log({getExpert: state.getProfile});
    return{
        get_Expert_List:state.getExpertList,
        get_profile:state.getProfile
    }
}

export default connect(mapStateToProps,{
    getExpertList,
    getProfile
})(FindExpert);

因此,在getExpertList的数据中,我通过传递单个用户的用户ID来调用getProfile。但在this.props.getProfile(expertUSer.UserId);中,我收到错误

  

getProfile不是函数

erro

这里应该做什么?

2 个答案:

答案 0 :(得分:2)

我看到您在getProfile中传递了connect但是......它未导入。的xD

尝试:

import {getExpert,getExpertList, getProfile} from './actions/ProfilePageActions.js';

答案 1 :(得分:1)

将当前上下文传递给地图:

this.state.experts.map(function(expertUSer) {...}, this)

所以你的getExpertInfo()看起来像这样:

getExpertInfo(){
    var i=1;
    if (this.state.experts && this.state.experts.length) {
        this.test = this.state.experts.map((expertUSer)=> {
            this.props.getProfile(expertUSer.UserId);
            console.log(this.props.get_profile);
            return(
                <ThemeProvider theme={theme} key={i++}>
                    <Card className='experts'>

                        <CardTitle title={expertUSer.Name}
                            subtitle={expertUSer.Designation}/>
                        <CardText>{expertUSer.Country}</CardText>
                    </Card>
                </ThemeProvider>
                )
        }, this);

        return this.test;
    }
    return null;
}