2017-06-13 91 views
0

我试图显示一个加载微调而axios获取请求正在进行,并且这样做我相信我需要从请求回调。我调用get请求componentDidMount()像这样:Axios回调获取反应

componentDidMount() { 
    this.props.fetchCloudProperties(() => { 
    this.setState({ dim : false }); 
    }); 
} 

如果希望当数据被检索到,调光器(微调)将离开基于关闭该seState通话。我有回帖的帖子,但从来没有做过一个get。我对数据取得动作是:

export function fetchCloudProperties(callback) { 
    const VARIABLE_URL = '/cloud/properties'; 
    const request  = axios.get(`${ROOT_URL}${VARIABLE_URL}`) 
    .then(request => { 
     return ({ 
     type : FETCH_CPS, 
     payload : request 
     }); 
    }).catch((error) => { 
     console.log(error); 
    }) 
} 

眼下控制台抱怨“操作必须是纯对象”,这我不知道这意味着什么,而且我已经看到了抱怨“未处理拒绝”所以不太确定。

编辑:我有一个使用一个回调createCloudProperty方法,它工作正常:

export function createCloudProperty(values, callback) { 
    const VARIABLE_URL = '/cloud/property'; 
    const request  = axios.post(`${ROOT_URL}${VARIABLE_URL}`, values) 
    .then(() => callback()); 

    return ({ 
    type : CREATE_CP, 
    payload : request 
    }); 
} 

是从所谓:

onSubmit(values) { 
    this.props.createCloudProperty(values,() => { 
    this.props.history.push('/cloudproperties'); 
    }); 
} 
+0

您是否在使用redux或flux或其他? fetchCloudProperties是动作创造者吗?如果你正在做异步操作,你需要使用'redux-thunk'这样的东西来返回动作创建者的承诺。 –

+0

[Actions必须是React/Redux中的普通对象?](https://stackoverflow.com/questions/39693161/actions-must-be-plain-objects-in-react-redux) –

+0

@AndyRay我是根据我的理解,使用'redux-promise',并且是'fetchCloudProperties()'是动作创建者。 – erp

回答

0

你不可错过的请求/响应因为它不是一个普通的对象。您应该使用响应数据。

export function fetchCloudProperties(callback) { 
    const VARIABLE_URL = '/cloud/properties'; 
    const request  = axios.get(`${ROOT_URL}${VARIABLE_URL}`) 
    .then(response => { 
     return ({ 
     type : FETCH_CPS, 
     payload : response.data 
     }); 
    }).catch((error) => { 
     console.log(error); 
    }) 
} 
+0

这提供了与以前相同的错误。我更新了我的问题,我是如何为一个帖子做的,而且工作正常。 – erp