simmerjs
simmerjs copied to clipboard
TypeError - Cannot read property 'length' of undefined
I am using simmer.js since yesterday and noticed this error coming up quite a lot.
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 []
}
}
}