Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for Cannot read property 'testUndefined' of undefined in

I tried code follow:

@Component({
  selector: 'test-content',
  template: '
    <div *ngIf="sv.name.notExist.testUndefined != undefined">
    {{sv.name.notExist.testUndefined}}
    ',
  directives: [FORM_DIRECTIVES]
})

The variable sv.name.notExist.testUndefined is undefined, but i check it with *ngIf and the result is error with message: "TypeError: Cannot read property 'testUndefined' of undefined in [sv.name.notExist.testUndefined != undefined in ..."

Please help me check variable undefined with *ngIf.

like image 981
Nguyen Avatar asked Jan 08 '16 09:01

Nguyen


People also ask

How do you know if a property Cannot be read undefined?

To solve the "Cannot read properties of undefined" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to access a property at a non-existent index after using the getElementsByClassName() method.

How do you solve Cannot read property of undefined react?

The "Cannot read property 'props' of undefined" error occurs when a class method is called without having the correct context bound to the this keyword. To solve the error, define the class method as an arrow function or use the bind method in the classes' constructor method.

How do you check Cannot read properties of null?

To solve the "Cannot read property 'value' of null" error, make sure you aren't accessing the value property on a null value, e.g. a non-existent DOM element. An element with the provided id does not exist in the DOM, so the getElementById method returns null .

What does Cannot read property ID of undefined mean?

The TypeError: Cannot read property of undefined is one of the most common type errors in JavaScript. It occurs when a property is read or a function is called on an undefined variable. Install the JavaScript SDK to identify and fix these undefined errors.


1 Answers

I think that you should use the elvis operator

<div *ngIf="sv?.name?.notExist?.testUndefined">

This link could give you more details: https://angular.io/docs/ts/latest/guide/template-syntax.html. See the section "The Elvis Operator ( ?. ) and null property paths".

Hope it helps you, Thierry

like image 156
Thierry Templier Avatar answered Nov 03 '22 01:11

Thierry Templier