ERROR TypeError: Cannot read property 'length' of undefined
K

6

15

There is an error in this part of my code

<img src="../../../assets/gms-logo.png" alt="logo" routerLink="/overview" alt="website icon">

But when I checked the assets folder, gms-logo.png is still there and in angular-cli.json, assets is also there. The path is also correct.

Recently though, I've been working on Search function. So my hypothesis is,

Has the program started searching even if the user is still not focused on the input type? How do I fix this?

Below is my html for Search and the showing of its suggestion segment

<input type="text" placeholder="Search" (keyup)="onSearch($event.target.value)">        
<div class="suggestion"  *ngIf="results.length > 0">
     <div *ngFor="let result of results ">
          <a href="" target="_blank">
                {{ result.name }}
          </a>
     </div>
</div>

Below is my component

results: Object;



 onSearch(name) {
            this.search
            .searchEmployee(name)
            .subscribe(
                name => this.results = name,//alert(searchName),this.route.navigate(['/information/employees/']),
                error => alert(error),
                );
}
Krissie answered 22/6, 2017 at 3:6 Comment(1)
you can access your img like this "assets/gms-logo.png"Duarte
S
33

You need to initialize your results variable as an array.

In your component, add:

results = [];

Another option is to change your suggestion div's *ngIf statement to check if results is defined:

<div class="suggestion"  *ngIf="results">
   <div *ngFor="let result of results ">
          <a href="" target="_blank">
                {{ result.name }}
       </a>
   </div>
</div>
Sterner answered 22/6, 2017 at 3:13 Comment(6)
Thank you. I've set result into an object. That's why. ThanksKrissie
No problem - keep at it!Sterner
Another option: results?.length > 0.Ballistics
@Ballistics Was looking everywhere for the fix since other solutions didnt work for Observable object array. But "?." fixed it instantly! Thanks a ton!Airlie
I added private userListing: User[]=[]; and then working. ThanksMitchell
We may simply use *ngif="results?.length"Decoct
B
13

The safe navigation operator ( ?. ) and null property paths

The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths. Here it is, protecting against a view render failure if the currentHero is null.

So in your example you can also use The safe navigation operator ( ?. ):

<div class="suggestion"  *ngIf="results?.length > 0">
    <div *ngFor="let result of results ">
        <a href="" target="_blank">
            {{ result.name }}
        </a>
    </div>
</div>
Brute answered 20/1, 2019 at 17:55 Comment(0)
K
5

Initializing the variable to empty solved my issue.

 DoctorList: any[] = [];
Kenspeckle answered 14/6, 2019 at 22:13 Comment(0)
D
4

I got stuck in a similar situation where even after assigning results as an array ( as shown below ), the error persisted.

results: Array<any>;

Using '?' ( Safe Navigation Operator ) worked out well for me.

*ngIf="results?.length"

The Safe Navigation Operator (?) can be used to prevent Angular from throwing errors while trying to access the properties of an object that doesn't exist.

This example will evaluate the length only when a value of results is not Null or Undefined.

Decarbonize answered 19/3, 2020 at 7:37 Comment(0)
J
2

I got same issue. I found 2 ways to fix it

  1. Assign result as an Array

    result = []

in html

*ngIf="results.length"
  1. Use ?

    *ngIf="results?.length"

Jermayne answered 18/3, 2020 at 7:51 Comment(0)
L
0

you can just initialise your array in declaration :

    result: Array<any>;

if the probleme style persiste you should check of null in your method lik this :

onSearch(name) {
        this.search
        .searchEmployee(name)
        .subscribe(
            name =>
if(name!=null){
this.results = 
name,//alert(searchName),this.route.navigate(['/information/employees/']),
}

            error => alert(error),
            );
}
Loggia answered 17/3, 2019 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.