首页 > 解决方案 > Angular 7 Promise 效果不佳?一次成功块,错误块

问题描述

我正在将我的应用程序 Angular 2(Http) 转换为 Angular 7(HttpClient) 我不明白为什么会这样。我的代码

     service.ts

  return this.http.get(serviceUrl).toPromise().then((data:any) => {
            return data;
        })

组件.ts

this.myService.getList()
    .then((rmanFmvRulesDefList: any) => {
      $.unblockUI();
      console.log('list',rmanFmvRulesDefList);
      this.datasource = rmanFmvRulesDefList.content;
      this.rmanFmvRulesDefList = rmanFmvRulesDefList.content;


      if (this.rmanFmvRulesDefList.length > 0) {

        for (let i = 0; i < rmanFmvRulesDefList.content.length; i++) {
          this.rmanFmvRulesDefList1.push({
            'fmvRuleName':  rmanFmvRulesDefList.content[i].fmvRuleName,
            'fmvCategory':  rmanFmvRulesDefList.content[i].fmvCategory
          })
        }

        /*  for (let i = 0; i < rmanFmvRulesDefList.content.length; i++) {
        this.rmanFmvRulesDefList1.push({
          'fmvRuleName':  rmanFmvRulesDefList.content[i].fmvRuleName,
          'fmvCategory':  rmanFmvRulesDefList.content[i].fmvCategory,
          'fmvType': rmanFmvRulesDefList.content[i].fmvType,
          'fmvRuleStatus':  rmanFmvRulesDefList.content[i].fmvRuleStatus,
          'fmvPrice':  rmanFmvRulesDefList.content[i].fmvPrice ,
          'min':  formatNumber(rmanFmvRulesDefList.content[i].min , '1.2-2'),
          'median': formatNumber(rmanFmvRulesDefList.content[i].median , '1.2-2'),
          'max': formatNumber(rmanFmvRulesDefList.content[i].max , '1.2-2')
        })
      }*/ 
      }

      console.log(this.rmanFmvRulesDefList1, 'rman fm list')

    }).catch((err: any) => {
      console.log('erre block')
          this.growlMsgs=[{severity:'error', summary:'Error Message', detail:'Error occured while getting data!!'}];
           $.unblockUI();
       });

从我的服务中,我得到了 100 个列表项

问题:我的success(then), error(then) 在这种情况下一次阻止调用

  1. 如果我准备具有两个属性的数组(this.rmanFmvRulesDefList1),它工作正常

    2. 但是如果我准备了具有 8 个属性的数组,它将进入 catch 块,并执行代码

    请任何人帮助我这里发生了什么

标签: angularangular-promiseangular7angular-httpclient

解决方案


推荐阅读