Leon Gaban Leon Gaban - 1 month ago 22
Javascript Question

2nd chained API not getting called (React + Axios)

I'm trying to chain 2 api calls, first is a POST which creates a new service, 2nd is a call to UPDATE that service with the correct user/admin.

However it's never making it to

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))
}


Thoughts? How could this be re-written?

Answer Source

.then takes a function as a parameter and not a function call. Try changing the chained then to

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