Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError: null is not an object (evaluating 's.filters') with pagefind.debouncedSearch #441

Closed
earthboundkid opened this issue Sep 18, 2023 · 7 comments

Comments

@earthboundkid
Copy link

If I pass any options to pagefind.debouncedSearch, it throws an error, TypeError: null is not an object (evaluating 's.filters'), but it doesn't throw that error if I call pagefind.search with the same options.

@earthboundkid
Copy link
Author

I just manually recreate debouncedSearch, like

const asyncSleep = async (ms = 100) => {
  return new Promise((r) => setTimeout(r, ms));
};

let currentSearchID = 0;

// See https://github.com/CloudCannon/pagefind/issues/441
async function debouncedSearch(
  pf,
  term,
  options = {},
  debounceTimeoutMs = 300,
) {
  const thisSearchID = ++currentSearchID;
  await asyncSleep(debounceTimeoutMs);

  if (thisSearchID !== currentSearchID) {
    return null;
  }

  const searchResult = await pf.search(term, options);
  if (thisSearchID !== currentSearchID) {
    return null;
  }
  return searchResult;
}

it doesn't throw an error, which suggests to me there's some shenanigans going on with this being corrupted somehow.

@earthboundkid
Copy link
Author

Adding pf.preload(term, options); (the equivalent of this.preload(term, options);) to the debouncedSearch above breaks it again. I think there's something wrong with preload's instance map where it can't deal with options when they're set.

@danpls
Copy link
Contributor

danpls commented Sep 18, 2023

You're right. I can reproduce this. The search function seems to hit the following if condition causing it to bail out and return null, which is exactly the same null that is responsible for the TypeError reported from the console.

if (options.preload) {
log(`Preload — bailing out of search operation now.`);
return null;
}

I am unsure of the purpose of options.preload, but my guess is that it's been mistakenly set in this case. Removing the if condition from above seems to fix it for me, however I am not yet comfortable with the code base, so I doubt this is going to be the appropriate solution.

@bglw
Copy link
Contributor

bglw commented Sep 19, 2023

Ahh I see what has happened here.

async preload(term: string, options: PagefindSearchOptions = {}) {
options.preload = true;
await this.search(term, options);
}

The preload function is mutating the options object, rather than passing a new one down. Which causes the now-mutated options object to be passed to the search function, which returns null 😩

Will fix

@danpls
Copy link
Contributor

danpls commented Sep 19, 2023

Can confirm. This fixes it on my side.

@earthboundkid
Copy link
Author

I’ll go ahead and close this issue, since it looks like #444 fixed it.

@bglw
Copy link
Contributor

bglw commented Nov 16, 2023

Released in v1.0.4 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants