ghiden / angucomplete-alt

Autocomplete Directive for AngularJS. A fork of Daryl Rowland's angucomplete (https://github.com/darylrowland/angucomplete) with some extra features.
https://ghiden.github.io/angucomplete-alt
MIT License
943 stars 496 forks source link

Error: str is undefined #282

Open sashazykov opened 9 years ago

sashazykov commented 9 years ago
  1. Getting this error when I press backspace after selecting some object.

Error: str is undefined
link/scope.inputChangeHandler@http://localhost:9000/bower_components/angucomplete-alt/angucomplete-alt.js:695:1

angucomplete-alt 2.0.2

sashazykov commented 9 years ago

Error only appears if maxlength is less than length of selected element title.

ghiden commented 9 years ago

Why do you want the maxlength to be less than the size of possible input string length?

sashazykov commented 9 years ago

It just happened and I didn't understand what was the reason of the exception. At least it should fail with the correct error message.

svemir commented 8 years ago

To me this is happening regardless of max value. If I type a character and then delete it, it gets to that place inputChangeHandler and tries to get str.length but str is null.

It might be something in my config or callbacks that is messing it up, but the default templates send searchStr to that function, and scope.searchStr is set to null in various places in the code.

There were several pull requests and commits that seem to try to handle this for specific cases, but it seems that all it needs is some fool-proofing, perhaps like this (I can submit pull request if you agree and I am not missing something here)

scope.inputChangeHandler = function(str) {
  var strlen = str ? str.length : 0;
  if (strlen < minlength) {
    cancelHttpRequest();
    clearResults();
  }
  else if (strlen === 0 && minlength === 0) {
    scope.searching = false;
    showAll();
  }
  // etc.
ghiden commented 8 years ago

Do you see errors in this example too? http://ghiden.github.io/angucomplete-alt/#example10

svemir commented 8 years ago

No, I do not get the error in any of the examples. It seems to be caused by some of the listeners I have that affect the search string. If I change the template from ng-change="inputChangeHandler(searchStr)" to ng-change="inputChangeHandler(searchStr || '')" it works fine.

Still, seeing that other places in the directive already do stuff like ...!scope.searchStr || scope.searchStr.length... it seems that it should not be assuming that searchStr will never be a null.

jesusr commented 8 years ago

This could happen if your 'title' field (not the 'search' field) is longer than the limit. It was my case.

kipusoep commented 7 years ago

I'm also experiencing this issue.