简体   繁体   English

TypeError:无法读取未定义的属性“地图”<angular 8></angular>

[英]TypeError: Cannot read property 'map' of undefined<Angular 8 >

I have an ag-grid populated from a service on click of a Search button , which invokes the API.我在单击Search button时从服务中填充了一个 ag-grid,它调用 API。 I am using map() function to create a new object from the original API object.我正在使用map() function 从原始 API ZA8CFDE6331BD59EB2AC96F8911 创建一个新的 object Are filtering and formatting required properties from original when creating a new object?创建新的 object 时,是否过滤和格式化所需的原始属性? New object is then set to the grid for the data population.然后将新的 object 设置为用于数据填充的网格。

Issue: When I clicked the first time on the Search button, though the Network shows the correct data fetch, nothing is populated to the UI due to the error:问题:当我第一次单击“搜索”按钮时,虽然网络显示正确的数据获取,但由于错误,UI 中没有填充任何内容:

TypeError: Cannot read property 'map' of undefined. TypeError:无法读取未定义的属性“地图”。

During the second click, data is populated into the Grid perfectly .在第二次单击期间,数据完美地填充到网格中。 Note that second click doesn't cause this error .请注意,第二次单击不会导致此错误

this._reportersService.getReporters()
            .subscribe(         
                    (data=> this.reporters = data),                       
                    (error => {
                                console.log(error);
                                this.errBlock = true;
                                this.errText = error.message;
                              })
                    )
            var res = {
                gridReporters: this.reporters.map(function(v) {
                  var fullName = v.TITLE+' '+v.FIRST_NAME+' '+v.MIDDLE_NAME+' '+v.LAST_NAME;
                  var address = v.STREET+' '+v.CITY+' '+v.STATE
                  return {
                    FULLNAME: fullName,
                    ADDRESS:address,
                    COUNTRY:v.COUNTRY,
                    POSTCODE:v.POSTCODE,
                    PHONE:v.PHONE,
                    EMAIL:v.EMAIL,
                    QUALIFICATION:v.QUALIFICATION,
                    INSTITUTION:v.INSTITUTION,
                    DEPARTMENT:v.DEPARTMENT
                  };
                })    


}
  var objString = JSON.stringify(res);
  objString = objString.split('{"gridReporters":').join('');
  objString = objString.split('}]}').join('}]');
  console.log("last str:"+objString);
   this.reporterGridOptions.rowData = JSON.parse(objString);
  this.reporterGridOptions.api.setRowData(this.reporterGridOptions.rowData);
}

Additionally, pls note:另外,请注意:

public reporters: Reporters[] ;

export class Reporters {
    REPORTER_ID: string;
    CLIENT_ID: string;
    TITLE: string;
    FIRST_NAME: string;
    MIDDLE_NAME:string;
    LAST_NAME: string;
    STREET: string;
    CITY: string;
    STATE: string;
    COUNTRY: string;
    PHONE: string;
    EMAIL: string;
    INSTITUTION: string;
    DEPARTMENT: string;
    QUALIFICATION: string;
    CREATED_BY:string;
    CREATED_DATE:Date;
    IS_PRIMARY_CONTACT:number;
    POSTCODE:string;
    STATUS:string;
    UPDATED_BY:string;
    UPDATED_DATE:string;
    }

All relevant code is enclosed, Pls help me with this.随附所有相关代码,请帮助我。 Thanks in advance.提前致谢。 ASJ. ASJ。

When you execute map() function on this.reporters it is undefined,because the subscribe function has not yet executed (asynchronous behaviour of js(ts)).Do something like below.当您在 this.reporters 上执行map() this.reporters时,它是未定义的,因为订阅 function 尚未执行(js(ts) 的异步行为)。执行如下操作。

this._reportersService.getReporters()
            .subscribe(         
                    (data=> {this.reporters = data ; // apply map() function here on this.reporters  }),                       
                    (error => {
                                console.log(error);
                                this.errBlock = true;
                                this.errText = error.message;
                              })
                    )

You should manipulate your data when it is available like:-您应该在数据可用时对其进行操作,例如:-

const res;
this._reportersService.getReporters()
        .subscribe(         
                (data=> {this.reporters = data;
                 res = {
            gridReporters: this.reporters.map(function(v) {
              var fullName = v.TITLE+' '+v.FIRST_NAME+' '+v.MIDDLE_NAME+' '+v.LAST_NAME;
              var address = v.STREET+' '+v.CITY+' '+v.STATE
              return {
                FULLNAME: fullName,
                ADDRESS:address,
                COUNTRY:v.COUNTRY,
                POSTCODE:v.POSTCODE,
                PHONE:v.PHONE,
                EMAIL:v.EMAIL,
                QUALIFICATION:v.QUALIFICATION,
                INSTITUTION:v.INSTITUTION,
                DEPARTMENT:v.DEPARTMENT
              };
            }) 
           }
          } 
                 ),                       
                (error => {
                            console.log(error);
                            this.errBlock = true;
                            this.errText = error.message;
                          })
                )

Try this尝试这个

You need to put your code of.map() into the last stage of successfully stage.您需要将代码 of.map() 放入成功阶段的最后阶段。 Like this..像这样..

 .subscribe(
    data => {
      this.reporters = data;
      this.isWait = true;
    },
    err => {
      this.errorMessage = err;
      this.isWait = false;
    }  ,
    () =>  {  this.isWait = false;
                  // Your this.reporters.map() code goes here.
              }
  );

First stage of subscribe () is getting api data and passed it to your reporters property. subscribe () 的第一阶段是获取 api 数据并将其传递给您的记者财产。

Second stage for any error handling of which occurred in while Calling data from api.从 api 调用数据时发生的任何错误处理的第二阶段。

Third stage is successful stage.第三阶段是成功阶段。 Called only when first stage is successfully called.仅在成功调用第一阶段时调用。 So your reporters property now has all data you needed.因此,您的记者财产现在拥有您需要的所有数据。

Note here, isWait is Boolean property, used for enabling or disabling Loader/ Spinners indication to user when time consuming to download data from api.请注意,isWait 是 Boolean 属性,用于在从 api 下载数据耗时时向用户启用或禁用 Loader/Spinners 指示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular:TypeError:无法读取未定义的属性“map” - Angular: TypeError: Cannot read property 'map' of undefined Angular TypeError:无法使用@Input 装饰器读取未定义的属性“映射” - Angular TypeError: Cannot read property 'map' of undefined with @Input decorator 错误TypeError:无法读取未定义角度4的属性“地图” - ERROR TypeError: Cannot read property 'map' of undefined angular 4 类型错误:无法使用 Angular v6 读取未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined with Angular v6 类型错误:无法读取未定义的属性“地图”:firebase 和 angular 日历 - TypeError: Cannot read property 'map' of undefined : firebase with angular calendar 错误类型错误:无法读取未定义的属性“地图” - ERROR TypeError: Cannot read property 'map' of undefined Angular,TypeError:无法读取未定义的属性“值” - Angular, TypeError: Cannot read property 'value' of undefined Angular,TypeError:无法读取未定义的属性&#39;toLowerCase&#39; - Angular, TypeError: Cannot read property 'toLowerCase' of undefined 类型错误:无法读取未定义的属性“电子邮件”(Angular) - TypeError: Cannot read property 'email' of undefined (Angular) TypeError:无法读取未定义Angular的属性&#39;indexOf&#39; - TypeError: Cannot read property 'indexOf' of undefined Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM