我获取一个名为project的对象,该对象包含一个数组QS和两个ID,我希望为它们中的每个派遣一个新动作。第一次调度正确返回数组
.then((project) => dispatch(lastProjectFetchDataSuccess(project.qs)))
,但其他两个返回未定义? 我要提取的对象:
{qs:[objects],qpId: "an ID",projectId: "another ID"}
动作:
export function lastProjectFetchData(url, owner) {
return dispatch => {
dispatch(lastProjectIsLoading(true));
fetch(url, {
method: "POST",
body: JSON.stringify({
owner: "charlie",
projectName: "charliesprojekt"
}),
headers: { "Content-Type": "application/json" }
})
.then(response => {
if (!response.ok) {
throw Error(response.statusText);
}
dispatch(lastProjectIsLoading(false));
return response;
})
.then(response => response.json())
.then(project => dispatch(lastProjectFetchDataSuccess(project.qs))) // returns the array
.then(project => dispatch(qpIdFetchDataSuccess(project.qpId))) // returns undefined
.then(project => dispatch(projectIdFetchDataSuccess(project.projectId))) // returns undefined
.catch(() => dispatch(lastProjectHasErrored(true)));
};
}
答案 0 :(得分:2)
您没有从第三个project
回调返回then
,因此以下then
回调将得到undefined
。
您可以改为:
.then(response => response.json())
.then(project => {
dispatch(lastProjectFetchDataSuccess(project.qs));
return project;
})
.then(project => {
dispatch(qpIdFetchDataSuccess(project.qpId))
return project;
})
.then(project => {
dispatch(projectIdFetchDataSuccess(project.projectId));
return project;
})
.catch(() => dispatch(lastProjectHasErrored(true)));
您也可以在同一then
回调中完成所有操作:
.then(response => response.json())
.then(project => {
dispatch(lastProjectFetchDataSuccess(project.qs));
dispatch(qpIdFetchDataSuccess(project.qpId));
dispatch(projectIdFetchDataSuccess(project.projectId));
})
.catch(() => dispatch(lastProjectHasErrored(true)));
答案 1 :(得分:0)
您将调用返回到flex-grow: 1
而不是第三个dispatch
中的项目,因此以下then
接收到then
的结果,该结果可能是{ {1}}。