所以我在整个应用程序中都以相同的方式使用订阅,但是这次做的事情很奇怪。这是我的订阅代码。
componentWillReceiveProps(nextProps) {
if (
nextProps.applications !== undefined &&
!nextProps.applications.loading
) {
this.props.applications.subscribeToMore({
document: JOB_APPLIED_SUBSCIPTION,
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) {
return prev;
}
const newApplication = subscriptionData.data.jobApplied;
if (
!prev.jobApplications.find(
a => a._id === newApplication._id
)
) {
return {
...prev,
jobApplications: [
{ ...newApplication },
...prev.jobApplications
]
};
}
return prev;
},
onError: err => console.log(err)
});
}
}
这部分来自我的渲染
const { loading } = this.props.applications;
if (!loading) {
const { jobApplications } = this.props.applications;
const applicantId = jobApplications.map(
user => user.user._id
);
let hasApplied = applicantId.includes(_id);
return (
<RoundedButton
text={hasApplied ? "APPLIED" : "APPLY NOW"}
color={colors.white}
backgroundColor={hasApplied ? colors.darkGray : colors.green}
borderColor={hasApplied ? colors.darkGray : colors.green}
handleOnPress={() => this.onJobApply(job, hasApplied)}
/>
);
}
}
现在,如果我尝试进行突变,订阅不会出现错误,但是当我在console.log(this.props.applications)
之前const applicantId
时,我可以看到data: []
开头是空的,因为没有对象已创建。在创建对象之后,订阅将更新并删除,在应用程序中,我会看到以下错误TypeError: undefined is not an object (evaluating 'this.props.applications.jobApplications.map')
知道为什么吗?