具有两个链式请求的操作。 "Cannot read property then of undefined" 错误
Action with two chained requests. "Cannot read property then of undefined" error
我的大多数 api 调用都是从这样的操作中调用的函数
操作:
export const fetchSomethingAction = () => {
return (dispatch) => {
dispatch({ type: types.FETCH_SOMETHING_REQUEST })
return api.fetchSomething()
.then((response) => {
dispatch({ type: types.FETCH_SOMETHING_SUCCESS, data: response.data })
})
.catch(error => {
dispatch({ type: types.FETCH_SOMETHING_FAILURE, error })
})
}
}
axios 请求是这样的:
export const fetchSomething = () => {
return axios({
method: 'GET',
url: `${api}/endpoint`
});
}
现在我需要发出两个链式请求,如果两个 请求中的任何一个失败
,则让操作分派一个错误
那么我如何链接请求来实现这一点?
我在做例子
export const fetchSomething = () => {
axios({
method: 'GET',
url: `${api}/endpoint1`
})
.then(res => {
return fetchSomething2(res.param)
})
.catch(err => return err)
}
const fetchSomething2 = (param) => {
return axios({
method: 'GET',
url: `${api}/endpoint1?param=${param}`
});
}
但是我在行动中得到了Cannot read property 'then' of undefined
。
我是否需要两个操作,每个功能一个?这是正确的方法吗?
谢谢
您的 fetchSomething
函数有一个用大括号定义的箭头函数,但没有 return 语句,因此隐式 return 未定义。将其更改为不使用大括号,或者使用 return 语句。
没有大括号:
export const fetchSomething = () =>
axios({
method: 'GET',
url: `${api}/endpoint1`
})
.then(res => {
return fetchSomething2(res.param)
})
.catch(err => return err)
与 return:
export const fetchSomething = () => {
return axios({
method: 'GET',
url: `${api}/endpoint1`
})
.then(res => {
return fetchSomething2(res.param)
})
.catch(err => return err)
}
这是一个较小的示例,您可以使用它来理解差异:
> a = () => 1; a()
1
> a = () => { 1 }; a()
undefined
> a = () => { return 1 }; a()
1
我的大多数 api 调用都是从这样的操作中调用的函数
操作:
export const fetchSomethingAction = () => {
return (dispatch) => {
dispatch({ type: types.FETCH_SOMETHING_REQUEST })
return api.fetchSomething()
.then((response) => {
dispatch({ type: types.FETCH_SOMETHING_SUCCESS, data: response.data })
})
.catch(error => {
dispatch({ type: types.FETCH_SOMETHING_FAILURE, error })
})
}
}
axios 请求是这样的:
export const fetchSomething = () => {
return axios({
method: 'GET',
url: `${api}/endpoint`
});
}
现在我需要发出两个链式请求,如果两个 请求中的任何一个失败
,则让操作分派一个错误那么我如何链接请求来实现这一点?
我在做例子
export const fetchSomething = () => {
axios({
method: 'GET',
url: `${api}/endpoint1`
})
.then(res => {
return fetchSomething2(res.param)
})
.catch(err => return err)
}
const fetchSomething2 = (param) => {
return axios({
method: 'GET',
url: `${api}/endpoint1?param=${param}`
});
}
但是我在行动中得到了Cannot read property 'then' of undefined
。
我是否需要两个操作,每个功能一个?这是正确的方法吗? 谢谢
您的 fetchSomething
函数有一个用大括号定义的箭头函数,但没有 return 语句,因此隐式 return 未定义。将其更改为不使用大括号,或者使用 return 语句。
没有大括号:
export const fetchSomething = () =>
axios({
method: 'GET',
url: `${api}/endpoint1`
})
.then(res => {
return fetchSomething2(res.param)
})
.catch(err => return err)
与 return:
export const fetchSomething = () => {
return axios({
method: 'GET',
url: `${api}/endpoint1`
})
.then(res => {
return fetchSomething2(res.param)
})
.catch(err => return err)
}
这是一个较小的示例,您可以使用它来理解差异:
> a = () => 1; a()
1
> a = () => { 1 }; a()
undefined
> a = () => { return 1 }; a()
1