question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MatTable: Cannot read property 'nativeElement' of undefined

See original GitHub issue

Bug, feature request, or proposal:

Bug

What is the expected behavior?

MatTable to render

What is the current behavior?

MatTable throws error

core.js:1365 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'nativeElement' of undefined
TypeError: Cannot read property 'nativeElement' of undefined
    at new CdkTable (table.js:466)
    at new MatTable (table.js:27)
    at createClass (core.js:10353)
    at createDirectiveInstance (core.js:10200)
    at createViewNodes (core.js:11657)
    at callViewAction (core.js:12091)
    at execComponentViewsAction (core.js:12000)
    at createViewNodes (core.js:11685)
    at createRootView (core.js:11546)
    at callWithDebugContext (core.js:12912)
    at new CdkTable (table.js:466)
    at new MatTable (table.js:27)
    at createClass (core.js:10353)
    at createDirectiveInstance (core.js:10200)
    at createViewNodes (core.js:11657)
    at callViewAction (core.js:12091)
    at execComponentViewsAction (core.js:12000)
    at createViewNodes (core.js:11685)
    at createRootView (core.js:11546)
    at callWithDebugContext (core.js:12912)
    at resolvePromise (zone.js:809)
    at resolvePromise (zone.js:775)
    at eval (zone.js:858)
    at ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:3979)
    at ZoneDelegate.invokeTask (zone.js:420)
    at Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)
    at <anonymous>

What are the steps to reproduce?

Providing a StackBlitz reproduction is the best way to share your issue.
StackBlitz starter: https://goo.gl/wwnhMV

What is the use-case or motivation for changing an existing behavior?

Getting MatTable to work

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

Package.json

{
  "name": "admin-app",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "build-dev": "ng build --base-href=/ngx/index/ --deploy-url=~/scripts/ngapp/",
    "build-prod": "ng build --base-href=/ngx/index/ --deploy-url=~/scripts/ngapp/ --prod --aot --no-progress",
    "test": "ng test --sourcemaps=false",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.3",
    "@angular/cdk": "^5.2.0",
    "@angular/common": "^5.2.3",
    "@angular/compiler": "^5.2.3",
    "@angular/core": "^5.2.3",
    "@angular/forms": "^5.2.3",
    "@angular/http": "^5.2.3",
    "@angular/material": "^5.2.0",
    "@angular/platform-browser": "^5.2.3",
    "@angular/platform-browser-dynamic": "^5.2.3",
    "@angular/router": "^5.2.3",
    "core-js": "^2.4.1",
    "font-awesome": "^4.7.0",
    "hammerjs": "^2.0.8",
    "instantsearch.js": "^2.5.0",
    "rxjs": "^5.5.2",
    "uniqid": "^4.1.1",
    "zone.js": "^0.8.20"
  },
  "devDependencies": {
    "@angular/cli": "^1.6.7",
    "@angular/compiler-cli": "^5.2.3",
    "@angular/language-service": "^5.2.3",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "codelyzer": "^4.1.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.4.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "node-sass": "^4.7.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "2.4.2"
  }
}

Is there anything else we should know?

It looks like the issue is when a new CdkTable is created:

image

Code:

Component.html

<mat-table [dataSource]="dataSource">

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
  </ng-container>

  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

Component.ts

import { AfterViewInit, Component, OnInit } from '@angular/core';
import { PartnersService } from '../../../shared-services/partners.service';


@Component({
  selector: 'app-partners',
  templateUrl: './partners.component.html',
  styleUrls: ['./partners.component.scss'],
  providers: [ PartnersService ]
})
export class PartnersComponent implements OnInit, AfterViewInit {
  displayedColumns = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;
  constructor() {

  }
  ngAfterViewInit() {
  }
  ngOnInit() {
  }

}

const ELEMENT_DATA: any[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
  {position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'},
  {position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'},
  {position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'},
  {position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'},
  {position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'},
  {position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'},
  {position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'},
  {position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'},
  {position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'},
  {position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'},
];

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
marc-wilsoncommented, Feb 6, 2018

Well, nevermind. I found the issue. Somehow, my target property in tsconfig.json had the value of ‘es2016’. Changing it to ‘es5’ fixed the issue

Causes error:

{
  "compileOnSave": true,
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2016",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

Doesn’t cause error:

{
  "compileOnSave": true,
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}
15reactions
jpremkumarcommented, Mar 12, 2018

I also face this issue by changing the target to es5 fix the issue. But I’ve some dependency on es6, that code is breaking now.

Is there any workaround to make it work with target: es6?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'nativeElement' of undefined In Angular 7
I want to implement data table in my angular app but it shows an error like Cannot read property 'nativeElement ...
Read more >
cannot read property 'nativeelement' of undefined ionic 4
error typeerror cannot read property 'nativeelement' of undefined angular 9. Sometimes instead of null it will say undefined. An example will be: Uncaught ......
Read more >
Angular: Mattable Giving Error "Cannot Read Property 'Find ...
Hey I am currently trying to use the mattable. my application I get ERROR TypeError: Cannot read property 'nativeElement' of undefined. Angular2 Cannot...
Read more >
Cannot read property 'nativeElement' of undefined - Ionic Forum
I am using ArcGIS Map using angular and am getting error to render my HTML Div tag and error is “Cannot read property...
Read more >
Angular Material Data Table: A Complete Example
<mat-table class="lessons-table mat-elevation-z8" ... nativeElement.value, ... Cannot read property 'sortChange' of undefined ??
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found