通过调用端点填充 angular 对象

populating angular object by calling an endpoint

我想用我将从一个端点获得的数据填充对象的一部分,然后将该填充的对象传递给另一个端点,但我最终得到了未定义的对象,我认为这是因为它需要一个虽然要从我的端点获取该数据,但不确定如何修复它,这是下面的函数:

upsetSavedReport(pagePath: string, saveReportContent: ReportSaveReportContext, fields: Array<{ isChecked: boolean, name: string }>, mySavedReport: ReportMySavedReportContext) {
try {
  let reportType = this.reportRouteFactory.getLabel2ByKey(pagePath);
  if (reportType) {
    let filterContent = saveReportContent.filter;
    let report = this.findReportById(saveReportContent.reportId, mySavedReport);

    this.deleteDuplicateReportByName(mySavedReport, saveReportContent);
    if (!report) {

    report.ReportName = saveReportContent.reportName;
    report.ViewFields = [];
    this.nrcService.getUserInfo().then(data => {
      //The problem is here I get the data but doesnt populate the field
      report.SendTo = [data && data[0] && data[0].emailaddress && data[0].emailaddress.split(/[,;]/)[0]] || null;
    });
      report = {
        ReportID: this.sharedService.generateGUID(),
        UID: globalConfig.settingsData.UserId || '',
        ReportName: saveReportContent.reportName,
        ReportType: reportType,
        Filters: [],
        ViewFields: []
      } as SavedReport;
      mySavedReport.reports.push(report);
    }
  }
} catch (e) { this.errorService.popupMessage(e); }

将您的其他代码放入 .then:

upsetSavedReport(pagePath: string, saveReportContent: ReportSaveReportContext, fields: Array<{ isChecked: boolean, name: string }>, mySavedReport: ReportMySavedReportContext) {
try {
  let reportType = this.reportRouteFactory.getLabel2ByKey(pagePath);
  if (reportType) {
    let filterContent = saveReportContent.filter;
    let report = this.findReportById(saveReportContent.reportId, mySavedReport);

    this.deleteDuplicateReportByName(mySavedReport, saveReportContent);
    if (!report) {

    report.ReportName = saveReportContent.reportName;
    report.ViewFields = [];
    this.nrcService.getUserInfo().then(data => {

//Inside the .then block
    report = {
            ReportID: this.sharedService.generateGUID(),
            UID: globalConfig.settingsData.UserId || '',
            ReportName: saveReportContent.reportName,
            ReportType: reportType,
            Filters: [],
            ViewFields: []
          } as SavedReport;

      report.SendTo = [data && data[0] && data[0].emailaddress && data[0].emailaddress.split(/[,;]/)[0]] || null;

          mySavedReport.reports.push(report);


    });

    }
  }
} catch (e) { this.errorService.popupMessage(e); }