下面的代码添加一条记录,然后重新读取表以获得添加的记录Id(变量'rid'),以便它可以用于添加子详细记录。Rid永远不会得到值。
似乎我需要某种类型的'then'逻辑或'}'调整,以便在我尝试返回并重新阅读主记录之前添加主记录。
任何想法?
…
copyReportCard = (e) => {
e.preventDefault();
var X = parseInt(this.state.selectedreportcardmasterid, 10)
ReportCardMasterService.getReportCardMasterById(X).then((res) =>{
let reportcardmaster = res.data;
let sitereportcardmaster = {
sitereportcardmasterCode: this.state.rCode,
sitereportcardmasterName: reportcardmaster.reportcardmasterName,
sitereportcardmastergradeLimit: reportcardmaster.reportcardmastergradeLimit,
sitereportcardmasterColora: reportcardmaster.reportcardmasterColora,
sitereportcardmasterColorb: reportcardmaster.reportcardmasterColorb,
sitereportcardmasterColorc: reportcardmaster.reportcardmasterColorc,
sitereportcardmasterColord: reportcardmaster.reportcardmasterColord,
sitereportcardmasterColore: reportcardmaster.reportcardmasterColore,
sitereportcardmastercpymasterId: reportcardmaster.reportcardmastercpymasterId,
sitereportcardmasterComments: '',
sitereportcardmasterCategorytotal: '',
sitemaster_sitemasterId:this.props.match.params.sitemaster_sitemasterid
};
SiteReportCardMasterService.createSiteReportCardMaster(sitereportcardmaster);
});
SiteReportCardMasterService.getSiteReportCardMaster().then((res) => {
this.setState({ sitereportcardmaster: res.data});
for (let i = 0; i < this.state.sitereportcardmaster.length; i++)
{
if (this.state.sitereportcardmaster[i].sitereportcardmasterCode === this.state.rCode){
this.setState({rid: this.state.sitereportcardmaster[i].sitereportcardmasterid})};
}
ReportCardDetailsService.getReportCardDetails().then((res) => {
this.setState({reportcarddetails: res.data});
for (let i = 0; i < this.state.reportcarddetails.length; i++)
{
var X = parseInt(this.state.selectedreportcardmasterid,10);
if (this.state.reportcarddetails[i].reportcardmaster_reportcardmasterId === X)
{
let sitereportcarddetails =
{
sitereportcarddetailsSequence: this.state.reportcarddetails[i].reportcarddetailsSequence,
sitereportcarddetailsDescription: this.state.reportcarddetails[i].reportcarddetailsDescription,
sitereportcarddetailsCriteria: this.state.reportcarddetails[i].reportcarddetailsCriteria,
sitereportcarddetailsWeight: this.state.reportcarddetails[i].reportcarddetailsWeight,
sitereportcarddetailsScore: this.state.reportcarddetails[i].reportcarddetailsScore,
sitereportcarddetailsWeightedscore: this.state.reportcarddetails[i].reportcarddetailsWeightedscore,
sitereportcarddetailsColor: this.state.reportcarddetails[i].reportcarddetailsColor,
sitereportcarddetailsComment: this.state.reportcarddetails[i].reportcarddetailsComment,
reportcarddetailcategory_reportcarddetailcategoryId: this.state.reportcarddetails[i].reportcarddetailcategory_reportcarddetailcategoryId,
sitereportcardmaster_sitereportcardmasterId: this.state.rid
}
SiteReportCardDetailsService.createSiteReportCardDetails(sitereportcarddetails);
}
}
});
});
}
…
很难说,但你可能只是想要一个额外的.then()
来连接第二阶段到第一阶段。
作为奖励,第二阶段的嵌套.then()
可以解嵌套。
你可能会得到这样的结果…
copyReportCard = (e) => {
e.preventDefault();
var X = parseInt(this.state.selectedreportcardmasterid, 10);
return ReportCardMasterService.getReportCardMasterById(X)
.then((res) => {
...
return SiteReportCardMasterService.createSiteReportCardMaster(...);
})
.then(() => {
return SiteReportCardMasterService.getSiteReportCardMaster();
})
.then((res) => {
this.setState({ 'sitereportcardmaster': res.data });
...
return ReportCardDetailsService.getReportCardDetails();
})
.then((res) => {
this.setState({ 'reportcarddetails': res.data });
for (let i = 0; i < this.state.reportcarddetails.length; i++) {
...
}
});
};
请注意,成功的测序依赖于前三个.then()
子句的返回承诺。
同时,copyReportCard()
现在返回Promise给它的调用者(尽管它看起来是顶级代码)。