simmerjs icon indicating copy to clipboard operation
simmerjs copied to clipboard

TypeError - Cannot read property 'length' of undefined

Open fruwe opened this issue 4 years ago • 0 comments

I am using simmer.js since yesterday and noticed this error coming up quite a lot.

Screen Shot 2020-03-29 at 17 34 32

After investigating a bit further it seems that the problem is this function:

var documentQuerySelector = function documentQuerySelector(scope) {
  var document = typeof scope.querySelectorAll === 'function' ? scope : scope.document ? scope.document : INVALID_DOCUMENT;
  return function (selector, onError) {
    try {
      return document.querySelectorAll(selector);
    } catch (ex) {
      // handle error
      onError(ex);
    }
  };
};

In case querySelectorAll(selector) raises an error undefined is returned and therefore query(selector, onError).length will fail.

For now I fixed it by setting my own query, roughly like this:

fetch(): string {
  const simmer = Simmer.default(undefined, undefined, this.documentQuerySelector())
  const selector = simmer(this.getElement())

  return selector
}

private documentQuerySelector(): Simmer.QueryEngine {
  const document = this.getDocument()

  return (selector: string, onError: (error: any) => void) => {
    try {
      return document.querySelectorAll(selector)
    } catch (ex) {
      // handle error
      onError(ex)
      return []
    }
  }
}

fruwe avatar Mar 29 '20 08:03 fruwe