question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

undefined in "searchResults" but present in "rawResults"

See original GitHub issue

Hi Luca,

me again^^

I have the following, quite strange behaviour.

You can add entries to the list of elements, that can be searched with mini-search. While you add the new entry, the query stays active.

The result should be, if the new entry matches the query, it should be displayed.

So, if the input data changes I perform search via a useEffect hook and display the new data.

But what happens is that despite having a match in the raw results (correct field, correct match, everything ok), the entry in searchResult is undefined causing a crash.

   useEffect(() => {
        if (data && !isFirstRender.current) {
            removeAll();
            addAll(data);
        }

        isFirstRender.current = false;

    }, [data]);

    useEffect(() => {
        if (data) {
            search(filter.query, {
                filter: filterOptions.categoryField && filter.categories.length > 0 ? matchCategory : undefined,
            });
        }
    }, [data, filter, filterOptions, matchCategory]);

The order of execution is correct, double checked on that.

Result looks like:

rawResults

[
    {
        "id": "6a901953144c411580520ed07B4567",
        "terms": [
            "einholung"
        ],
        "score": 8.600445452294665,
        "match": {
            "einholung": [
                "custom.bezeichnung"
            ]
        },
        "custom.kategorien": [
            "Gefahr in Verzug",
            "Sicherheit",
            "Qualität"
        ]
    },
    {
        "id": "h1662s",
        "terms": [
            "einholungsbums"
        ],
        "score": 4.152082359120152,
        "match": {
            "einholungsbums": [
                "custom.bezeichnung"
            ]
        },
        "custom.kategorien": [
            "Gefahr in Verzug"
        ]
    }
]
[
    {
        "id": "6a901953144c411580520ed07B4567",
        "datum": "2020-09-17T20:34:56.170914Z",
        "custom": {
            "id": "6a901953144c411580520ed07a21ef39",
            "erstelldatum": "2020-09-17T20:34:56.170914Z",
            "baumassnahmenId": "45042621",
            "bezeichnung": "Einholung weiterer Informationen",
            "kategorien": [
                "Gefahr in Verzug",
                "Sicherheit",
                "Qualität"
            ],
    .....
    },
    undefined -> Where the second element should be.
]

Help would be highly appreciated. I’m kind of confused…

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
florianmatzcommented, May 18, 2021

Hi Luca:

Here you go: https://codepen.io/florianmatz/pen/xxqEaGE?

Steps to reproduce:

  • Codepen’s console
  • Enter “Zum Testen” into the search field (“Enter query”)
  • You’ll get a “No results” warning
  • Now switch the dataset; choose “Dataset 1” from select
  • App crashes -> discrepancy between rawResults and searchResults (see console) as described in the issue

What’s completely weird:

If you switch the datasets BEFORE you perform the reproduction steps, it works like a charm. So, what’s important to reproduce: Only with data, that has not been indexed before… So make sure you’ve reloaded the codepen before testing it.

Here you find a short screen recording, if my description was too confusing… 😃 https://we.tl/t-3Xpiz6bMq6

It kind of seems, that the indexing is done, but the mapping of terms -> results is not ready yet… (if that makes any sense at all 😛)

0reactions
lucaongcommented, May 19, 2021

Great news @florianmatz , the problem was indeed in react-minisearch. It was using useState instead of useRef to store local values such as the map of documents by ID: the problem is that setting the state is asynchronous, while adding/removing things to the index is synchronous by default. Therefore, the effective order of update of the index and the map of documents by ID was swapped, and under certain conditions this would surface as the bug you found.

The new version 3.0.3 of react-minisearch fixes the problem, and seems to work well with your app: https://codepen.io/lucaong/pen/yLMVxxy

Thanks again for reporting this, and for the reproduction example on codepen! I am closing the issue, but feel free to comment on it if you find out that it is not completely solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vscode/settingsEditor2.ts at main - GitHub
these setters need to exist because this extends from EditorPane ... searchResults: ISearchResult[] | null; rawResults: ISearchResult[] | null; ...
Read more >
Why is my search query returning undefined, while my page ...
Why is my search query returning undefined, while my page populates with the ... It fixes the url, but my search results are...
Read more >
WWE Raw Results: Winners, Grades, Reaction and Highlights ...
Welcome to Bleacher Report's coverage and recap of WWE Raw on December 19. Last week saw Adam Pearce fire Bobby Lashley, shocking the...
Read more >
Bird Conservation International: Volume 26 - Issue 1
The importance of spring hunting in Malta on European Turtle-Dove Streptopelia turtur and Common Quail Coturnix coturnix populations.
Read more >
WWE Raw Results: Winners, News And Notes On December ...
WWE Raw results as Bobby Lashley is fired by Adam Pearce. ... seem like he had found a joint that belonged to Matt...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found