我有一个处理文件上传的Angular接口,在提交按钮之后,它将这个文件保存在MySQL数据库中。我想在这个提交方法中进行另一个调用,但第二个http调用需要实体persist (file)的id
,我不知道如何从上传的文件中检索特定的id。
这是我的提交方法:
onSubmit() {
const statementFile = new StatementFile();
// *******
// here are some business logic
// *******
// this will send POST request and save it to MySQL.
this.statementFileService.postStatementFile(statementFile).subscribe(
{
next : (response) => {
alert('You have been submitted your statement file, check MySQL');
},
error: (err) => {
alert(`there was a problem: ${err.message}` );
}
}
);
// this is the second http request I want to call but it needs particularly the id of that statementFile uploaded.
this.activitiWorkflowService.createPreccess(statementFile.statementFileId).pipe(takeUntil(this._unsubscribeAll), debounceTime(3000)).subscribe(
(data: any) => data
);
}
这是我的StatementFile类:
export class StatementFile{
statementFileId: number;
dateReception: Date;
label: string;
}
您可以使用switchMap操作符来链接请求。statementFile。statementFileId或响应。statementFileId或其他来自postStatementFile请求的东西,你将使用它来调用下一个请求。
this.statementFileService.postStatementFile(statementFile)
.pipe(
switchMap(response => this.activitiWorkflowService.createPreccess(statementFile.statementFileId).pipe(takeUntil(this._unsubscribeAll)))
)
.subscribe({
next : (data: any) => data,
error: (err) => {
alert(`there was a problem: ${err.message}` );
}
}
);