Piotr Sikora - Logo - Automatyzacja procesów | AI | JavaScript | Front End | Team Leader
  • Home
  • Services
    • Process Automation
    • AI for Lawyers
  • Blog
  • Quotes
  • Contact
PL/EN

Fix Angular 2 Pipes: Resolve TypeError

  • Home
  • Blog
  • Fix Angular 2 Pipes: Resolve TypeError
Fix Angular 2 Pipes: Resolve TypeError

By Piotr Sikora

  • angular

  • 3 min read

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.

Share this article

TwitterLinkedInFacebook

Tags:

  • #angular

  • #javascript

  • #js

Categories

after-hours(1)AI(7)ai-en(1)angular(4)automatic-tests(1)Automation(2)cryptography(1)css(8)CyberSecurity(2)Development(6)DevOps(1)events(3)javascript(11)n8n(10)ollama(1)security(2)seo(1)

Recent Posts

Testing Kimi Code: First Impressions from Web and CLI

Development

Testing Kimi Code: First Impressions from Web and CLI

Why You Shouldn't Cram Multiple Webhooks Into One n8n Workflow

Automation

Why You Shouldn't Cram Multiple Webhooks Into One n8n Workflow

DRY, WET, AHA: Finding the Right Balance in Code Reuse

Development

DRY, WET, AHA: Finding the Right Balance in Code Reuse

API vs Webhook: Understanding the Difference

Development

API vs Webhook: Understanding the Difference

RTCROS Framework: Structure Your Prompts for Better AI Results

AI

RTCROS Framework: Structure Your Prompts for Better AI Results

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
View Templates

• Auto-Categorize Blog Posts with AI

• Collect LinkedIn Profiles

• Export WordPress Posts for SEO

Tags

activepiecesafter-hoursahrefsaiAI cost reductionai-agentsai-automationangularantigravityapiAqua Securityarcade-gamesarchitectureautomationbackendbest-practicescadillacs-and-dinosaurschatbotchatgptCI/CD
“Rule number one: never lose money. Rule number two: never forget rule number one.”
Warren Buffett
View more quotes

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

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.

Piotr Sikora | ai | n8n | javascript | python

Let's get to know each other!

Get in touch with me
Piotr Sikora | Process Automation  | AI Implementation 🤖 | Technology Consulting
Quick links
  • Home
  • Blog
  • Contact
Contact
  • piotr.sikora.ck@gmail.com
  • +48 505 684 661

© Piotr Sikora 2026 | All Rights Reserved