多个帖子请求(Axios | React)

时间:2016-09-27 13:35:56

标签: javascript api reactjs redux axios

如何从axios调用多个API?我需要调用2个不同的API来在我的前端显示一些数据。

这是我的代码

  

操作/ index.js

export function userAuth() {

    return (dispatch, getState) => {
      axios.post('url', {
        usuario: 'test',
        password: 'test'
      })
      .then((response) => {
        // console.log(response.data);
        dispatch( { type: USER_AUTH, payload: response.data } )
      })
      .catch(function (error) {
        console.log(error);
      });
    }
}

export function userLog() {

    return (dispatch, getState) => {
      axios.post('url', {
        clientId: '1',
        sensor: 'test',
        dateStart: '2016-09-03 00:00:00"',
        dateEnd: '2016-09-03 23:59:59'

      })
      .then((response) => {
        console.log(response.data);
        dispatch( { type: USER_LOG, payload: response.data } )
      })
      .catch(function (error) {
        console.log(error);
      });
    }
}

axios.all([userAuth(), userLog()])
  .then(axios.spread(function (acct, perms) {
    console.log(userLog());
  }))
  

减速/ userlog.js

export function userLog(state = initialState, action) {

    switch (action.type) {
        case USER_LOG:
            return Object.assign({}, state, {list: action.payload})
        default:
            return state
    }

}

但是在我的api调用中的第二个console.log(),没有打印任何内容,我缺少什么?或者我做错了什么?...

第二个问题是,在完成所有操作之后,我如何编辑API调用的主体?我需要一个充满活力的身体,我的意思是

1 API =>响应带有一些文本的数组。

2 API =>身体将从(1 API)更改为foreach数组?

**

0 个答案:

没有答案
相关问题