【发布时间】:2020-06-17 13:30:39
【问题描述】:
我有一个函数,其中包含一些承诺链,但这不是重点。
当我运行某个突变的refetch 时,它给了我Uncaught (in promise) TypeError: Cannot read property 'refetch' of undefined。
奇怪的是,如果我在它之前删除一个突变,它就会起作用。代码如下:
Promise.all(this.props.questionnaireData.map(({ kind, id }): Promise<any> => {
const responses = this.props.formData[kind];
return this.props.updateQuestionnaire(id, responses);
})).then(() => {
this.props.finishAssessment(this.props.assessmentId)
.then(() => {
track('Assessment -- Finished', {
'Assessment Kind' : this.props.assessmentKind,
'Assessment Id' : this.props.assessmentId,
});
if (this.props.assessmentKind === 'INITIAL_ASSESSMENT') {
this.props.getCompletedInitialAssessment.refetch().then(() => {
Router.replace(routes.LoadingAssessmentResults.to, routes.LoadingAssessmentResults.as);
});
this.submitEmailNotifications();
} else if(this.props.assessmentKind === 'GOAL_CHECK_IN') {
Router.replace(routes.MemberProgressDashboard.to, routes.MemberProgressDashboard.as);
} else {
Router.replace(routes.MemberDashboard.to, routes.MemberDashboard.as);
}
});
});
错误发生在this.props.getCompletedInitialAssessment.refetch(),我不知道为什么。但是,当我删除 this.props.finishAssessment(this.props.assessmentId) 时,只有这样才能重新获取。
基本上:
Promise.all(this.props.questionnaireData.map(({ kind, id }): Promise<any> => {
const responses = this.props.formData[kind];
return this.props.updateQuestionnaire(id, responses);
})).then(() => {
track('Assessment -- Finished', {
'Assessment Kind' : this.props.assessmentKind,
'Assessment Id' : this.props.assessmentId,
});
if (this.props.assessmentKind === 'INITIAL_ASSESSMENT') {
this.props.getCompletedInitialAssessment.refetch().then(() => {
Router.replace(routes.LoadingAssessmentResults.to, routes.LoadingAssessmentResults.as);
});
this.submitEmailNotifications();
} else if(this.props.assessmentKind === 'GOAL_CHECK_IN') {
Router.replace(routes.MemberProgressDashboard.to, routes.MemberProgressDashboard.as);
} else {
Router.replace(routes.MemberDashboard.to, routes.MemberDashboard.as);
}
});
将使重新获取工作。否则它会抱怨它不知道 refetch 是什么。
对于 Apollo,我使用的是 graphql HOC,它看起来像这样:
graphql(getCompletedInitialAssessment, {
name : 'getCompletedInitialAssessment',
options : { variables: { status: ['Finished'], limit: 1 } },
}),
graphql(updateQuestionnaire, {
props: ({ mutate }) => ({
updateQuestionnaire: (id, responses) => {
let normalized = {};
for (let res in responses) {
let num = +responses[res];
// If the value is a stringified numuber, turn it into a num
// otherwise, keep it a string.
normalized[res] = Number.isNaN(num) ? responses[res] : num;
}
const input = {
id,
patch: { responses: JSON.stringify(normalized) },
};
return mutate({
variables: { input },
});
},
}),
}),
graphql(finishAssessment, {
props: ({ mutate }) => ({
finishAssessment: (id) => {
const input = { id };
return mutate({
variables : { input },
refetchQueries : ['getMemberInfo'],
});
},
}),
}),
我尝试过甚至重写它以使用 async/await,但问题仍然存在:
try {
await Promise.all(this.props.questionnaireData.map(({ kind, id }): Promise<any> => {
const responses = this.props.formData[kind];
return this.props.updateQuestionnaire(id, responses);
}));
const finishAssessmentRes = await this.props.finishAssessment(this.props.assessmentId);
console.log(finishAssessmentRes)
if (this.props.assessmentKind === 'INITIAL_ASSESSMENT') {
const res = await this.props.getCompletedInitialAssessment.refetch();
console.log(res);
this.submitEmailNotifications();
Router.replace(routes.LoadingAssessmentResults.to, routes.LoadingAssessmentResults.as);
} else if(this.props.assessmentKind === 'GOAL_CHECK_IN') {
Router.replace(routes.MemberProgressDashboard.to, routes.MemberProgressDashboard.as);
} else {
Router.replace(routes.MemberDashboard.to, routes.MemberDashboard.as);
}
} catch (error) {
console.error(error);
}
老实说,我不知道发生了什么,也不知道为什么 refetch 不起作用。重构为钩子会有帮助吗?有人知道吗?
【问题讨论】:
-
该错误实际上与重新获取无关,它告诉您没有
this.props.getCompletedInitialAssessment可以调用重新获取。 -
可以理解。我不知道为什么会在调用
finishAssessment后发生这种情况。它会覆盖getCompletedInitialAssessment吗? -
好吧,我假设
getCompletedInitialAssessment是作为prop 传递给组件的父状态的一部分?它是如何改变的?什么时候?你知道设置状态是异步的吗?
标签: javascript reactjs graphql react-apollo