cibernox / ember-power-select

The extensible select component built for ember.
http://www.ember-power-select.com
Other
540 stars 379 forks source link

TypeError Cannot read properties of null (reading 'length') on `this.searchText.length > 0` #1854

Closed johanrd closed 1 week ago

johanrd commented 2 months ago

Hi. In sentry I have some reports on Cannot read properties of null (reading 'length') on this.searchText.length > 0 on get results()

Not sure if I am stretching the usage here, but anyways, I think ember-power-select should be resilient towards this.

Two fixes I can think of:

1) Optional chaining on all consumers of this.searchText.length (i.e. this.searchText?.length)

  get results(): any[] {
-    if (this.searchText.length > 0) {
+    if (this.searchText?.length > 0) {
      if (this.args.search) {
        return toPlainArray(this._searchResult || this.options);

2) Nullish coalescing on all assignments to this.searchText to ensure it is not null or undefined?

 @action
  _search(term: string): void {
    if (this.searchText === term) return;
-    this.searchText = term;
+    this.searchText = term ?? '';
    if (!this.args.search) {
      this.lastSearchedText = term;
      this._resetHighlighted();
    }
  }
mkszepp commented 2 months ago

I'm not sure if this is really an issue of EPS, because from type we allow only string for searchTerm. This means that when the app is not passing a string, its an incorrect using, which should be fixed inside app.

In the typed world you issue will never appear, but in JS its always everything possible.

johanrd commented 2 months ago

ok, thanks. FYI: I am in a fully typed world (all .gts) and do not get any glint errors in my component usage. Still this issue appears in sentry.

mkszepp commented 2 months ago

Hmm... can you share the example like you are using? Because with TS it should bring an error

mkszepp commented 1 week ago

Closing in favor of no aditional feedback