通过调用端点来填充角对象

时间:2019-01-15 00:57:53

标签: angular asynchronous angular5

我想用从端点获取的数据填充对象的一部分,然后将该填充的对象传递给另一个端点,但是我最终得到的是未定义的对象,我认为这是因为要花一些时间才能完成从我的端点获取数据,不确定如何解决,这是下面的功能:

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); }

1 个答案:

答案 0 :(得分:1)

将其他代码放入.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); }