TypeError: Cannot read property 'length' of undefined

Hello Good Day,
I’m new in using Ionic 3 and still practicing and watching tutorials in youtube
I’ve been facing a problem or error regarding to my codes tho I’m just following what was posted in youtube.

In creating a Registration page (just a trial or practice)

Here’s the code (just as the same on the youtube tutorial):

import { Component } from ‘@angular/core’;

import { IonicPage, NavController, NavParams } from ‘ionic-angular’;

export class RegisterPage
{
username:string;
password:string;
repassword:string;

constructor(public navCtrl: NavController, public navParams: NavParams) {
}

ionViewDidLoad() {
console.log(‘ionViewDidLoad RegisterPage’);
}

register(){
if(this.username.length==0|| this.password.length==0 || this.repassword.length==0)
alert(“Please fill all fields”);
}
}

But I receive the error of:
“Runtime Error:
Cannot read ‘length’ of undefined”

“TypeError: Cannot read ‘length’ of undefined”

Please help! Thank you in advance

You need to initialize first

username:string = '';
password:string = '';
repassword:string = '';
1 Like

Sorry, I’m just starting to learn how to code/program. Thank you for this one. :smiley: