Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schematic(table): Cannot read property 'data' of undefined #15788

Closed
b-mi opened this issue Apr 11, 2019 · 4 comments · Fixed by #15800
Closed

schematic(table): Cannot read property 'data' of undefined #15788

b-mi opened this issue Apr 11, 2019 · 4 comments · Fixed by #15800
Assignees
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@b-mi
Copy link

b-mi commented Apr 11, 2019

What is the expected behavior?

Do not work, ends with error "Cannot read property 'data' of undefined"

What is the current behavior?

Without error

What are the steps to reproduce?

Providing a StackBlitz reproduction is the best way to share your issue.

StackBlitz starter: https://goo.gl/wwnhMV

Bad generated code:

  ngAfterViewInit() {
    this.dataSource = new InfoOProgDataSource(this.paginator, this.sort);
  }

Correct code:
  ngOnInit() {
    this.dataSource = new InfoOProgDataSource(this.paginator, this.sort);
  }

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Updated angular, material 7.3.7

Is there anything else we should know?

Better testing?

@Splaktar Splaktar changed the title Generated table schematic not work, Cannot read property 'data' of undefined schematic(table): Cannot read property 'data' of undefined Apr 11, 2019
@Splaktar
Copy link
Member

Do you have a full stack trace to share?

@Splaktar
Copy link
Member

OK, I was able to reproduce this:

ERROR TypeError: Cannot read property 'data' of undefined
    at TableComponent_Template (template.html:20)
    at checkView (core.js:8963)
    at componentRefresh (core.js:8772)
    at refreshChildComponents (core.js:7896)
    at refreshDescendantViews (core.js:7836)
    at checkView (core.js:8964)
    at componentRefresh (core.js:8772)
    at refreshChildComponents (core.js:7896)
    at refreshDescendantViews (core.js:7836)
    at renderComponentOrTemplate (core.js:8099)

@devversion
Copy link
Member

Issue can be fixed by using Angular's safe navigation operator when accessing the dataSource property.

https://github.com/angular/material2/blob/309d5644aa610ee083c56a823ce7c422988730e8/src/lib/schematics/ng-generate/table/files/__path__/__name%40dasherize%40if-flat__/__name%40dasherize__.component.html#L19-L24

Right now the [length] binding fails because dataSource is undefined until the view is initialized completely.

@devversion devversion added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Apr 11, 2019
Splaktar added a commit that referenced this issue Apr 11, 2019
fix exception when instantiating MatPaginator before dataSource defined
fix ExpressionChangedAfterItHasBeenCheckedError caused by above
- first initialize the template w/o dataSource & w/ a sort and paginator
- then get the references to the template's MatSort and MatPaginator
- then set the data source's sort and paginator
- then set the table's data source from the component

Fixes #15788
@crisbeto crisbeto added the has pr label May 5, 2019
Splaktar added a commit that referenced this issue May 13, 2019
fix exception when instantiating MatPaginator before dataSource defined
fix ExpressionChangedAfterItHasBeenCheckedError caused by above
- first initialize the template w/o dataSource & w/ a sort and paginator
- then get the references to the template's MatSort and MatPaginator
- then set the data source's sort and paginator
- then set the table's data source from the component

Fixes #15788
jelbourn pushed a commit that referenced this issue May 17, 2019
fix exception when instantiating MatPaginator before dataSource defined
fix ExpressionChangedAfterItHasBeenCheckedError caused by above
- first initialize the template w/o dataSource & w/ a sort and paginator
- then get the references to the template's MatSort and MatPaginator
- then set the data source's sort and paginator
- then set the table's data source from the component

Fixes #15788
@Splaktar Splaktar self-assigned this May 17, 2019
RudolfFrederiksen pushed a commit to RudolfFrederiksen/material2 that referenced this issue Jun 21, 2019
…r#15800)

fix exception when instantiating MatPaginator before dataSource defined
fix ExpressionChangedAfterItHasBeenCheckedError caused by above
- first initialize the template w/o dataSource & w/ a sort and paginator
- then get the references to the template's MatSort and MatPaginator
- then set the data source's sort and paginator
- then set the table's data source from the component

Fixes angular#15788
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants