Pathik Vejani
Pathik Vejani

Reputation: 4491

TypeError: Cannot read property 'filter' of undefined

I am working on angular 5 and onSubmit() of form I am getting below error:

ERROR TypeError: Cannot read property 'filter' of undefined

Below is onSubmit() of login.component.ts file:

onSubmit() {
        this.valid = true;
        const name = this.login.userName;
        sessionStorage.setItem('username', this.login.userName);
        const password = this.login.password;
        console.log(name);
        const user = this.users.filter(currUser => currUser.userName === name && currUser.password === password)[0]; // here I am getting an error!
        if (user) {
            this.isLoggedIn = 'true';
            sessionStorage.setItem('isLoggedIn', this.isLoggedIn);
            // this.router.navigate(['/products']);
        } else {
            this.isLoggedIn = 'false';
            sessionStorage.setItem('isLoggedIn', this.isLoggedIn);
            this.valid = false;
        }
    }

Upvotes: 2

Views: 4437

Answers (1)

Shane
Shane

Reputation: 669

this.users 

Is undefined, make sure you are assigning it elsewhere in the component

Upvotes: 1

Related Questions