Fix Angular 2 Pipes: Resolve TypeError

By Piotr Sikora

  • angular

Ive been trying to build simple filtering in application -> Show-ur-bugs (https://github.com/fedojo/show-ur-bugs).

Let me introduce the error environment firstly:   project-list.component.ts

import 'rxjs/add/operator/map';
import {Component, OnInit} from '@angular/core';
import {ProjectService} from "../../shared";
import {ProjectListSingleComponent} from "./project/project-list-single.component";
import {FilterData} from "../../filter-data.pipe";

@Component({
    moduleId: module.id,
    bindings: [ProjectService],
    selector: 'app-project-list',
    templateUrl: 'project-list.component.html',
    styleUrls: ['project-list.component.css'],
    directives: [ProjectListSingleComponent],
    pipes: [FilterData]

})

export class ProjectListComponent implements OnInit {
    projects;

    constructor(public projectService:ProjectService) {

    }

    getProjects() {
        this.projectService.getProjects()
            .subscribe(
                data => {
                    this.projects = data;
                },
                error => console.error('Error: ' + error[0]),
                () => {}
            )
    }

    ngOnInit() {
        this.getProjects();
    }

}

project-list.component.html

<input type="text" #filter (keyup)="0">

<project-list-single *ngFor="let project of (projects | filterData: filter.value)"
                         [project]="project"
                         class="project"></project-list-single>

filter-data.pipe.ts - ver 1

import {Pipe, PipeTransform} from '@angular/core';
import {ProjectItem} from './projects/index';

@Pipe({
    name: 'filterData'
})
export class FilterData implements PipeTransform {

    transform(value:ProjectItem[], args:string[]):any {
        if (value.length === 0) {
           return value;
        }

        var resultArray = [];

        for (let item of value) {
            if (item.name.match('^.*' + args[0] + '.*$')) {
                 resultArray.push(item);
            }
        }

        return resultArray;
    }

}

The error occured:

ORIGINAL EXCEPTION: TypeError: Cannot read property ‘length’ of undefined

But which element hasn't got length? After quick investigation in code it was object which was passed to the Pipe. It hasn't exist in moment when the Pipe was invoked. So here came an idea to resolve it:

filter-data.pipe.ts - ver 2

import {Pipe, PipeTransform} from '@angular/core';
import {ProjectItem} from './projects/index';

@Pipe({
    name: 'filterData'
})
export class FilterData implements PipeTransform {

    transform(value:ProjectItem[], args:string[]):any {

        if (typeof value === 'object') {
            var resultArray = [];

            if (args.length === 0) {
                for (let item of value) {
                    resultArray.push(item);
                }
            }

            else {
                for (let item of value) {
                    if (item.name.match('^.*' + args[0] + '.*$')) {
                        resultArray.push(item);
                    }
                }
            }

            return resultArray;
        }

    }

}

Why do you need this condition in pipe?

if (args.length === 0) {
                for (let item of value) {
                    resultArray.push(item);
                }
            }

When your array of objects will be loaded it will be automatically pushed to Pipe which checks the value from your input. When input is empty the result of the Pipe will be empty too so you wont see your objects in list.

Categories

Recent Posts

About Me

Piotr Sikora - Process Automation | AI | n8n | Python | JavaScript

Piotr Sikora

Process Automation Specialist

I implement automation that saves time and money, streamlines operations, and increases the predictability of results. Specializing in process automation, AI implementation, and workflow optimization using n8n, Python, and JavaScript.

n8n Workflows

n8n workflow automation templates

Explore my workflow templates on n8n. Ready-to-use automations for blog management, data collection, and AI-powered content processing.

3Workflow Templates

• Auto-Categorize Blog Posts with AI

• Collect LinkedIn Profiles

• Export WordPress Posts for SEO

Similar Articles

Discover more related content

DevMeeting Angular 2: Exploring the Future

DevMeeting Angular 2: Exploring the Future

Last weekend Ive been with my friends at DevMeeting about Angular 2.0 in Krakow.

Angular 2 - How to pass more parameters to Pipe

Angular 2 - How to pass more parameters to Pipe

Pipes are very important element of Angular 2 framework. Create your own pipe to transform and filter your data.

Angular Side Effects with @Effect & NgRx CRUD

Essential CRUD Snippets. Angular is giving great tools to build application and NgRx gives you opportunity to manage you app with state.