Skip to main content

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="">
    <img src="https://path_to_images//.jpg" width="380" height="266" alt=""/>
    <title></title>
    <subtitle class="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.

Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

Comments

Popular posts from this blog

Prop `className` did not match in next js app

I have written a sample code ( Github Link here ). this is a simple next js app, but giving me error when I refresh the page. This seems to be the common problem and I tried the fix provided in the internet but does not seem to fix my issue. The error is Warning: Prop className did not match. Server: "MuiBox-root MuiBox-root-1" Client: "MuiBox-root MuiBox-root-2". Did changes for _document.js, modified _app.js as mentioned in official website and solutions in stackoverflow. but nothing seems to work. Could someone take a look and help me whats wrong with the code? Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console