Ionic2 - TypeError: Cannot read property 'length' of undefined
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
Hello, thanks for opening an issue with us! Would you be able to provide a plunker that demonstrates this issue? Thanks for using Ionic!
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.
Solved, simply using the following:
if(!search.target.value){
let query = '';
} else {
let query = search.target.value;
}
Thanks to @ghenry22
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!
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
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
+1
+1
thanks bro :) +1
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.