Когда вы назначаете dict2 = dict1
, вы не делаете копию dict1
, это приводит к тому, что dict2
является просто другим именем для dict1
.
Чтобы скопировать изменяемые типы, такие как словари , используйте copy
/ deepcopy
модуля copy
.
import copy
dict2 = copy.deepcopy(dict1)
Попробуйте это
async method() {
const userInfo = await this.nrcService.getUserInfo();
if (!!userInfo) {
this.userInfo = userInfo;
}
if (!!this.userInfo) {
report = {
ReportType: reportType,
ReportID: id,
ReportFormat: format,
ReportName: `${reportType}_${id}`,
Filters: shouldNotParseFilters ? filterContent : [],
ViewFields: columns || [],
OrgName: this.userInfo[0].orgname,
FullName: this.userInfo[0].fullname
} as SavedReport;
if (!shouldNotParseFilters) {
this.reportFilterService.mapReportFilters(<ReportFilterContext>filterContent, report);
}
report.Filters.push({ 'maxitems': [-1] });
this.nrcService.downloadReport(report, fileName).subscribe({ error: err => this.nrcService.handleError(err) });
} else {
// ...
}
}
вы можете использовать async / await, что-то вроде
this.userInfo = await this.userInfo || this.nrcService.getUserInfo();
report = { ... }
В этом случае я бы предложил использовать async/await
для решения ваших задач:
// Add `async` here
async your_function_name () {
...
let report;
if (!this.userInfo) {
// Waits until your user info is loaded
this.userInfo = await this.nrcService.getUserInfo();
}
report = {
ReportType: reportType,
ReportID: id,
ReportFormat: format,
ReportName: `${reportType}_${id}`,
Filters: shouldNotParseFilters ? filterContent : [],
ViewFields: columns || [],
OrgName: this.userInfo[0].orgname,
FullName: this.userInfo[0].fullname
} as SavedReport;
if (!shouldNotParseFilters) {
this.reportFilterService.mapReportFilters(<ReportFilterContext>filterContent, report);
}
report.Filters.push({ 'maxitems': [-1] });
this.nrcService.downloadReport(report, fileName).subscribe({ error: err => this.nrcService.handleError(err) });
}
Если вы не знакомы с async/await
, см. здесь ; -) [ 117]
Другой вариант - использовать Обещания без async/await
, но это выглядит не так хорошо:
let report;
// Create a new promise, which resolves directly
let promise = new Promise(r => r());
if (!this.userInfo) {
// Assigns the loading promise to the promise variable
promise = this.nrcService.getUserInfo().then(data => {
this.userInfo = data;
});
}
// Waits until the promise is resolved
promise.then(() => {
report = {
ReportType: reportType,
ReportID: id,
ReportFormat: format,
ReportName: `${reportType}_${id}`,
Filters: shouldNotParseFilters ? filterContent : [],
ViewFields: columns || [],
OrgName: this.userInfo[0].orgname,
FullName: this.userInfo[0].fullname
} as SavedReport;
if (!shouldNotParseFilters) {
this.reportFilterService.mapReportFilters(<ReportFilterContext>filterContent, report);
}
report.Filters.push({ 'maxitems': [-1] });
this.nrcService.downloadReport(report, fileName).subscribe({ error: err => this.nrcService.handleError(err) });
});