ERROR TypeError: Cannot read property 'name' - Angular Material Table












1















I'm sending mutliple REST GET Calls to retrieve data from an external ressource.



I can see the requests sent out and returned with the related data. But after the last response I'm getting:
ERROR TypeError: Cannot read property 'name' of undefined
at Object.eval [as updateRenderer] (ProductcatalogComponent.html:14)



My assumption is that the way I'm storing the data is not right way to do it when collecting multiple Calls...



this is the function from my service.ts



  getData() {
return this.getProducts('/v1/catalog/products');
}

getProducts(url, dataSoFar = ): Observable<any> {
if (!url) {
return of (dataSoFar);
} else {
url = ZUORA_URL + url;
}
return this.http.get<any>(url, { headers }).pipe(
switchMap(p => this.getProducts( p.nextPage, [...dataSoFar, ...p.data]))
);
}


and here is how I call it from the component:



  ngOnInit() {
this.zuoraService.getData().subscribe(catalog => {
this.dataSource = new MatTableDataSource(<Catalog>catalog);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
}


this is the model:



export interface Catalog {
name: string;
id: string;
sku: string;
description: string;
IsTrialProduct__c: string;
}


and this is the html file



<div class="mat-horizontal-content-container">
<h1><i class="material-icons">dvr</i> Productcatalog</h1>
</div>

<div class="mat-elevation-z8">
<mat-form-field style="width: 100%">
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">

<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>

<ng-container matColumnDef="sku">
<th mat-header-cell *matHeaderCellDef mat-sort-header>SKU</th>
<td mat-cell *matCellDef="let row">{{row.sku}}</td>
</ng-container>

<ng-container matColumnDef="description">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Description</th>
<td mat-cell *matCellDef="let row">{{row.description}}</td>
</ng-container>

<ng-container matColumnDef="IsTrialProduct__c">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Trial</th>
<td mat-cell *matCellDef="let row">{{row.IsTrialProduct__c}}</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="openAccountDetails(row.ID)"></tr>
</table>

<mat-paginator #paginator
[length]="dataSource.data.length"
[pageIndex]="0"
[pageSize]="25"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>


How can I fix the issue? I will have to use the data at different components. The data won't change often --> what would the best way to prefetch the data once and cache it?










share|improve this question

























  • Can you provide a StackBlitz?

    – Prashant Pimpale
    Jan 20 at 6:34
















1















I'm sending mutliple REST GET Calls to retrieve data from an external ressource.



I can see the requests sent out and returned with the related data. But after the last response I'm getting:
ERROR TypeError: Cannot read property 'name' of undefined
at Object.eval [as updateRenderer] (ProductcatalogComponent.html:14)



My assumption is that the way I'm storing the data is not right way to do it when collecting multiple Calls...



this is the function from my service.ts



  getData() {
return this.getProducts('/v1/catalog/products');
}

getProducts(url, dataSoFar = ): Observable<any> {
if (!url) {
return of (dataSoFar);
} else {
url = ZUORA_URL + url;
}
return this.http.get<any>(url, { headers }).pipe(
switchMap(p => this.getProducts( p.nextPage, [...dataSoFar, ...p.data]))
);
}


and here is how I call it from the component:



  ngOnInit() {
this.zuoraService.getData().subscribe(catalog => {
this.dataSource = new MatTableDataSource(<Catalog>catalog);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
}


this is the model:



export interface Catalog {
name: string;
id: string;
sku: string;
description: string;
IsTrialProduct__c: string;
}


and this is the html file



<div class="mat-horizontal-content-container">
<h1><i class="material-icons">dvr</i> Productcatalog</h1>
</div>

<div class="mat-elevation-z8">
<mat-form-field style="width: 100%">
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">

<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>

<ng-container matColumnDef="sku">
<th mat-header-cell *matHeaderCellDef mat-sort-header>SKU</th>
<td mat-cell *matCellDef="let row">{{row.sku}}</td>
</ng-container>

<ng-container matColumnDef="description">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Description</th>
<td mat-cell *matCellDef="let row">{{row.description}}</td>
</ng-container>

<ng-container matColumnDef="IsTrialProduct__c">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Trial</th>
<td mat-cell *matCellDef="let row">{{row.IsTrialProduct__c}}</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="openAccountDetails(row.ID)"></tr>
</table>

<mat-paginator #paginator
[length]="dataSource.data.length"
[pageIndex]="0"
[pageSize]="25"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>


How can I fix the issue? I will have to use the data at different components. The data won't change often --> what would the best way to prefetch the data once and cache it?










share|improve this question

























  • Can you provide a StackBlitz?

    – Prashant Pimpale
    Jan 20 at 6:34














1












1








1


1






I'm sending mutliple REST GET Calls to retrieve data from an external ressource.



I can see the requests sent out and returned with the related data. But after the last response I'm getting:
ERROR TypeError: Cannot read property 'name' of undefined
at Object.eval [as updateRenderer] (ProductcatalogComponent.html:14)



My assumption is that the way I'm storing the data is not right way to do it when collecting multiple Calls...



this is the function from my service.ts



  getData() {
return this.getProducts('/v1/catalog/products');
}

getProducts(url, dataSoFar = ): Observable<any> {
if (!url) {
return of (dataSoFar);
} else {
url = ZUORA_URL + url;
}
return this.http.get<any>(url, { headers }).pipe(
switchMap(p => this.getProducts( p.nextPage, [...dataSoFar, ...p.data]))
);
}


and here is how I call it from the component:



  ngOnInit() {
this.zuoraService.getData().subscribe(catalog => {
this.dataSource = new MatTableDataSource(<Catalog>catalog);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
}


this is the model:



export interface Catalog {
name: string;
id: string;
sku: string;
description: string;
IsTrialProduct__c: string;
}


and this is the html file



<div class="mat-horizontal-content-container">
<h1><i class="material-icons">dvr</i> Productcatalog</h1>
</div>

<div class="mat-elevation-z8">
<mat-form-field style="width: 100%">
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">

<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>

<ng-container matColumnDef="sku">
<th mat-header-cell *matHeaderCellDef mat-sort-header>SKU</th>
<td mat-cell *matCellDef="let row">{{row.sku}}</td>
</ng-container>

<ng-container matColumnDef="description">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Description</th>
<td mat-cell *matCellDef="let row">{{row.description}}</td>
</ng-container>

<ng-container matColumnDef="IsTrialProduct__c">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Trial</th>
<td mat-cell *matCellDef="let row">{{row.IsTrialProduct__c}}</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="openAccountDetails(row.ID)"></tr>
</table>

<mat-paginator #paginator
[length]="dataSource.data.length"
[pageIndex]="0"
[pageSize]="25"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>


How can I fix the issue? I will have to use the data at different components. The data won't change often --> what would the best way to prefetch the data once and cache it?










share|improve this question
















I'm sending mutliple REST GET Calls to retrieve data from an external ressource.



I can see the requests sent out and returned with the related data. But after the last response I'm getting:
ERROR TypeError: Cannot read property 'name' of undefined
at Object.eval [as updateRenderer] (ProductcatalogComponent.html:14)



My assumption is that the way I'm storing the data is not right way to do it when collecting multiple Calls...



this is the function from my service.ts



  getData() {
return this.getProducts('/v1/catalog/products');
}

getProducts(url, dataSoFar = ): Observable<any> {
if (!url) {
return of (dataSoFar);
} else {
url = ZUORA_URL + url;
}
return this.http.get<any>(url, { headers }).pipe(
switchMap(p => this.getProducts( p.nextPage, [...dataSoFar, ...p.data]))
);
}


and here is how I call it from the component:



  ngOnInit() {
this.zuoraService.getData().subscribe(catalog => {
this.dataSource = new MatTableDataSource(<Catalog>catalog);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
}


this is the model:



export interface Catalog {
name: string;
id: string;
sku: string;
description: string;
IsTrialProduct__c: string;
}


and this is the html file



<div class="mat-horizontal-content-container">
<h1><i class="material-icons">dvr</i> Productcatalog</h1>
</div>

<div class="mat-elevation-z8">
<mat-form-field style="width: 100%">
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">

<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>

<ng-container matColumnDef="sku">
<th mat-header-cell *matHeaderCellDef mat-sort-header>SKU</th>
<td mat-cell *matCellDef="let row">{{row.sku}}</td>
</ng-container>

<ng-container matColumnDef="description">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Description</th>
<td mat-cell *matCellDef="let row">{{row.description}}</td>
</ng-container>

<ng-container matColumnDef="IsTrialProduct__c">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Trial</th>
<td mat-cell *matCellDef="let row">{{row.IsTrialProduct__c}}</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="openAccountDetails(row.ID)"></tr>
</table>

<mat-paginator #paginator
[length]="dataSource.data.length"
[pageIndex]="0"
[pageSize]="25"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>


How can I fix the issue? I will have to use the data at different components. The data won't change often --> what would the best way to prefetch the data once and cache it?







angularjs angular angular-material material-design






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 15:59







Malte

















asked Jan 18 at 23:40









MalteMalte

373




373













  • Can you provide a StackBlitz?

    – Prashant Pimpale
    Jan 20 at 6:34



















  • Can you provide a StackBlitz?

    – Prashant Pimpale
    Jan 20 at 6:34

















Can you provide a StackBlitz?

– Prashant Pimpale
Jan 20 at 6:34





Can you provide a StackBlitz?

– Prashant Pimpale
Jan 20 at 6:34












1 Answer
1






active

oldest

votes


















0














changed the service:



public getCatalog() {
console.log(this.catalog);
return this.catalog;
}

loadCatalog() {
this.fetchCatalog().subscribe(catalog => this.catalog.next(catalog));
}

public fetchCatalog(): Observable<Catalog> {
return this.http.get<Catalog>(ZUORA_URL + '/v1/catalog/products?page=1&pageSize=10', { headers })
.pipe(
expand(catalog => {
if (!catalog.nextPage) {
return EMPTY;
}
return this.http.get<Catalog>(ZUORA_URL + catalog.nextPage, { headers });
}),
map(catalog => catalog.products),
reduce((accData, data) => accData.concat(data), ),
);
}


added the factory to app.module.ts --> Data is loaded once on app build --> data doens't have to be reloaded any time a user changes the view



export function catalogProviderFactory(zuoraService: ZuoraService) {
return () => zuoraService.loadCatalog();
}


Component OnInit to build the table:



  ngOnInit() {
this.zuoraService.catalog$.subscribe(catalog => {
this.dataSource = new MatTableDataSource(catalog);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54262773%2ferror-typeerror-cannot-read-property-name-angular-material-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    changed the service:



    public getCatalog() {
    console.log(this.catalog);
    return this.catalog;
    }

    loadCatalog() {
    this.fetchCatalog().subscribe(catalog => this.catalog.next(catalog));
    }

    public fetchCatalog(): Observable<Catalog> {
    return this.http.get<Catalog>(ZUORA_URL + '/v1/catalog/products?page=1&pageSize=10', { headers })
    .pipe(
    expand(catalog => {
    if (!catalog.nextPage) {
    return EMPTY;
    }
    return this.http.get<Catalog>(ZUORA_URL + catalog.nextPage, { headers });
    }),
    map(catalog => catalog.products),
    reduce((accData, data) => accData.concat(data), ),
    );
    }


    added the factory to app.module.ts --> Data is loaded once on app build --> data doens't have to be reloaded any time a user changes the view



    export function catalogProviderFactory(zuoraService: ZuoraService) {
    return () => zuoraService.loadCatalog();
    }


    Component OnInit to build the table:



      ngOnInit() {
    this.zuoraService.catalog$.subscribe(catalog => {
    this.dataSource = new MatTableDataSource(catalog);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
    });





    share|improve this answer




























      0














      changed the service:



      public getCatalog() {
      console.log(this.catalog);
      return this.catalog;
      }

      loadCatalog() {
      this.fetchCatalog().subscribe(catalog => this.catalog.next(catalog));
      }

      public fetchCatalog(): Observable<Catalog> {
      return this.http.get<Catalog>(ZUORA_URL + '/v1/catalog/products?page=1&pageSize=10', { headers })
      .pipe(
      expand(catalog => {
      if (!catalog.nextPage) {
      return EMPTY;
      }
      return this.http.get<Catalog>(ZUORA_URL + catalog.nextPage, { headers });
      }),
      map(catalog => catalog.products),
      reduce((accData, data) => accData.concat(data), ),
      );
      }


      added the factory to app.module.ts --> Data is loaded once on app build --> data doens't have to be reloaded any time a user changes the view



      export function catalogProviderFactory(zuoraService: ZuoraService) {
      return () => zuoraService.loadCatalog();
      }


      Component OnInit to build the table:



        ngOnInit() {
      this.zuoraService.catalog$.subscribe(catalog => {
      this.dataSource = new MatTableDataSource(catalog);
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
      });





      share|improve this answer


























        0












        0








        0







        changed the service:



        public getCatalog() {
        console.log(this.catalog);
        return this.catalog;
        }

        loadCatalog() {
        this.fetchCatalog().subscribe(catalog => this.catalog.next(catalog));
        }

        public fetchCatalog(): Observable<Catalog> {
        return this.http.get<Catalog>(ZUORA_URL + '/v1/catalog/products?page=1&pageSize=10', { headers })
        .pipe(
        expand(catalog => {
        if (!catalog.nextPage) {
        return EMPTY;
        }
        return this.http.get<Catalog>(ZUORA_URL + catalog.nextPage, { headers });
        }),
        map(catalog => catalog.products),
        reduce((accData, data) => accData.concat(data), ),
        );
        }


        added the factory to app.module.ts --> Data is loaded once on app build --> data doens't have to be reloaded any time a user changes the view



        export function catalogProviderFactory(zuoraService: ZuoraService) {
        return () => zuoraService.loadCatalog();
        }


        Component OnInit to build the table:



          ngOnInit() {
        this.zuoraService.catalog$.subscribe(catalog => {
        this.dataSource = new MatTableDataSource(catalog);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
        });





        share|improve this answer













        changed the service:



        public getCatalog() {
        console.log(this.catalog);
        return this.catalog;
        }

        loadCatalog() {
        this.fetchCatalog().subscribe(catalog => this.catalog.next(catalog));
        }

        public fetchCatalog(): Observable<Catalog> {
        return this.http.get<Catalog>(ZUORA_URL + '/v1/catalog/products?page=1&pageSize=10', { headers })
        .pipe(
        expand(catalog => {
        if (!catalog.nextPage) {
        return EMPTY;
        }
        return this.http.get<Catalog>(ZUORA_URL + catalog.nextPage, { headers });
        }),
        map(catalog => catalog.products),
        reduce((accData, data) => accData.concat(data), ),
        );
        }


        added the factory to app.module.ts --> Data is loaded once on app build --> data doens't have to be reloaded any time a user changes the view



        export function catalogProviderFactory(zuoraService: ZuoraService) {
        return () => zuoraService.loadCatalog();
        }


        Component OnInit to build the table:



          ngOnInit() {
        this.zuoraService.catalog$.subscribe(catalog => {
        this.dataSource = new MatTableDataSource(catalog);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 23 at 15:29









        MalteMalte

        373




        373






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54262773%2ferror-typeerror-cannot-read-property-name-angular-material-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage