ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Ionic2 - TypeError: Cannot read property 'length' of undefined

Open HamzaLJ opened this issue 10 years ago • 10 comments

Ionic 2 - Beta9 made a major change on SearchBar component which affect several things.

I want to retrieve the length of the entered value on the searchBar; however, I was surprised that .length property is no longer defined.

I have migrated from Beta 7, which I had the below code working fine:

    search(search) {
      let query = search.value;
      console.log("Length of the query is ", query.length);
    }

Now, I am on Beta 10 the above code stopped working, so I made the necessary changes to match the new ones:

    search(search) {
       let query = search.target.value;
      console.log("Length of the query is ", query.length);
    }

Surprisingly, I received the below error:

TypeError: Cannot read property 'length' of undefined

Is .length method is no longer supported, or there is another way to retrieve the length or it's even a Bug?

Cordova CLI: 6.2.0 Ionic Framework Version: 2.0.0-beta.10 Ionic CLI Version: 2.0.0-beta.32 Ionic App Lib Version: 2.0.0-beta.18 ios-deploy version: 1.8.6 ios-sim version: 5.0.8 OS: Mac OS X El Capitan Node Version: v5.9.0 Xcode version: Xcode 7.3.1 Build version 7D1014

HamzaLJ avatar Jul 01 '16 03:07 HamzaLJ

Hello, thanks for opening an issue with us! Would you be able to provide a plunker that demonstrates this issue? Thanks for using Ionic!

jgw96 avatar Jul 01 '16 08:07 jgw96

I you haven't entered at least 1 character in the search box or if you just cleared the box then the target.value property is not set to an empty string, the property is completely removed (hence .length of undefined as target.value does not exist).

You can put something really simple in your search handler like below (assuming you are using something similar to the example in the docs.

if(!search.target.hasOwnProperty("value"){
 let query = '';
} else {
 let query = search.target.value;}

...then do your .length and everything else down here.

ghenry22 avatar Jul 01 '16 09:07 ghenry22

Solved, simply using the following:

  if(!search.target.value){
   let query = '';
  } else {
   let query = search.target.value;
  }

Thanks to @ghenry22

HamzaLJ avatar Jul 01 '16 17:07 HamzaLJ

Hello all, since this issue has now been fixed I will be closing this issue. Thanks @ghenry22 for helping! And, thanks for using Ionic everyone!

jgw96 avatar Jul 01 '16 19:07 jgw96

cordova run android ANDROID_HOME=C:\Users\Arumuganainar\AppData\Local\Android\sdk1 JAVA_HOME=C:\Program Files\Java\jdk1.8.0_141 Error: Cannot read property 'length' of undefined

[ERROR] Cordova encountered an error. You may get more insight by running the Cordova command above directly.

[ERROR] An error occurred while running cordova run android (exit code 1). how can i fix this? please help me

arumuganainar73 avatar Aug 07 '17 09:08 arumuganainar73

I have this problem too!

Ionic info: cli packages: (C:\dev2\mpTrackerGPS\node_modules)

@ionic/cli-plugin-cordova       : 1.6.2
@ionic/cli-plugin-ionic-angular : 1.4.1
@ionic/cli-plugin-proxy         : 1.4.1
@ionic/cli-utils                : 1.7.0
ionic (Ionic CLI)               : 3.7.0

global packages:

Cordova CLI : 7.0.1

local packages:

@ionic/app-scripts : 2.1.3
Cordova Platforms  : android 6.2.3 ios 4.2.1
Ionic Framework    : ionic-angular 3.6.0

System:

Node : v7.6.0
OS   : Windows 7
npm  : 5.3.0

raphitz avatar Aug 14 '17 18:08 raphitz

+1

amit7127 avatar Sep 06 '17 10:09 amit7127

+1

swapniljindal avatar Oct 31 '17 02:10 swapniljindal

thanks bro :) +1

numanayhan avatar Nov 03 '17 09:11 numanayhan

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 01 '18 19:09 ionitron-bot[bot]