第二个链式API未被调用(React + Axios)

时间:2017-08-22 22:17:35

标签: javascript reactjs axios

我试图连接2个api调用,首先是一个创建新服务的POST,第二个是使用正确的用户/ admin调用UPDATE服务。

然而,它永远不会进入api.updateUserRights

export const addService = (service, user, rights) => (dispatch) => {
  const params = {
    name: service,
    disabled: false,
    rights: rights
  };

  console.log('----------------- addService')
  console.log(' service', service)
  console.log(' user', user)
  console.log(' params', params)

  const addUserToService = (user) => api.updateUserRights(key, user);

  api.addService(service, params)
    // First Add the Service
    .then(res => {
      dispatch({
        type: actionTypes.ADD_SERVICE,
        payload: {
          service,
          rights
        }
      });
    })
    // Then Update the Admin to have the Service and Rights
    .then(addUserToService(user))
}

思考?怎么可以重写呢?

1 个答案:

答案 0 :(得分:2)

.then将函数作为参数而不是函数调用。 尝试将链式then更改为

    // Then Update the Admin to have the Service and Rights
.then(res => { addUserToService(user) } )