TVJS – Search functionality crashes when trying to show results

I’m working on a TVML application. One of the screens shows a search form for the user to enter a keyword. The text entered is compared to a previously existing JSON file that contains every item in existence. After the available results are filtered, these are used to build XML nodes by using an external XML file, and then they are imported into the search document, specifically into the section with Id “results”. The problem is that if I just type a search term directly, the search takes place. However, if I type letter by letter (allowing for a search after each letter) then the application crashes and I get the following error:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
terminating with uncaught exception of type NSException

(the index number and the range vary with different results)

Here are the files I currently have:

search.tvml

<?xml version="1.0" encoding="UTF-8" ?>
<document>
<head>
    <style>
        .company {
            font-size: 26;
        }
    </style>
</head>
<searchTemplate>
    <searchField />
    <collectionList>
        <grid>
            <header>
                <title>Search Results</title>
            </header>
            <section id="results">
                
            </section>
        </grid>
    </collectionList>
</searchTemplate>
</document>

searchResults.tvml

<lockup template="video.tvml" presentation="push" id="{{id}}">
    <img src="https://path_to_images/{{path}}/{{image}}.jpg" width="380" height="266" alt="{{name}}"/>
    <title>{{name}}</title>
    <subtitle class="company">{{company}}</subtitle>
</lockup>

SearchHandler.js

class SearchHandler {
    constructor(resourceLoader) {
        this._resourceLoader = resourceLoader;
        this.searchDatabase = "";
    }

    enableSearchForDoc(doc) {
        this._addSearchHandlerToDoc(doc);
    }

    _addSearchHandlerToDoc(doc) {
        var searchFields = doc.getElementsByTagName('searchField');
        for (var i = 0; i < searchFields.length; ++i) {
            var searchField = searchFields.item(i);
            var keyboard = searchField.getFeature("Keyboard");
            keyboard.onTextChange = this._handleSearch.bind(this, keyboard, doc);
        }
    }


    _handleSearch(keyboard, doc) {
    
        // Get the text entered
        var searchString = keyboard.text;
    
        // Blank results array
        var results = [];
    
        // Search only if text entered has more than one character
        if (searchString.length > 1) {
            results = this.searchVideosForString(searchString, this.searchDatabase);
        }
    
        // Get the result nodes (lockups) from the results obtained
        var resultNodes = this._renderResults(results);
    
        // Get a reference to the "results" section in search.tvml
        var resultContainer = doc.getElementById("results");
    
        // If such section exists
        if (resultContainer) {
            // remove previous results
            while (resultContainerChildren > 0 ) {
                resultContainer.removeChild(resultContainer.lastChild);
                resultContainerChildren--;
            }
        
            // Add each lockup node to the results section
            if (resultNodes.length > 0) {
                resultNodes.forEach(function(resultNode) {
                    doc.adoptNode(resultNode);
                    resultContainer.appendChild(resultNode);
                });
            }
        }
    }

    _renderResults(results) {
        var resourceLoader = this._resourceLoader;
    
        // Get an XML node (lockup) for each of the results found
        // Results will be used as data for the XML
        var rendered = results.map(function(result) {
            var doc = resourceLoader.getRenderedDocument("searchResults.tvml", result);
            return doc.documentElement;
        });
    
        // Return the array of lockup nodes
        return rendered;
    }

    searchVideosForString(searchString, data) {
        var sourceData = data;
    
        var results = []
    
        results = sourceData.filter(function(v) {
            var title = v.name;
            var show = v.showname;
            var company = v.company;
                                    
            return title.toLowerCase().includes(searchString.toLowerCase()) || show.toLowerCase().includes(searchString.toLowerCase()) || company.toLowerCase().includes(searchString.toLowerCase())
        });

        console.log("SEARCH RESULTS: ", results);

        return results;
    }
}

I have been monitoring the results of the searchVideosForString method using the Safari console, and what I have discovered is, if for example I’m trying to search for “Parry” which exists in the full results, if I type in the word “Par”, the console shows for Search Results an array with 18 objects. If I open the disclosure triangle, it shows each of those 18 objects. If I then add a character in the search to look for “Parr”, then the app crashes. The console shows for Search Results an array with 1 object, but if I open the disclosure triangle, it’s empty, I can’t see what the actual object is.

I have tried deleting sections of the code in the handleSearch method. If I remove everything below

var resultContainer = doc.getElementById("results");

then I can enter any text in the search box without any issues and the console shows the appropriate search results, no matter how many they are.

It seems the problem happens in this line:

doc.adoptNode(resultNode);

but I can’t figure out why nor where that index out of range exception is coming from.