DIYgod/RSSHub · error · TypeError

Invalid hits data received from API

Error message

Invalid hits data received from API

What it means

Thrown as a TypeError specifically when data.results[1].hits exists but is not an array. This is a secondary guard after the primary results-shape check (error 503) passes. The handler accesses results[1] (the 'Request' index results) and then calls .map() on hits, so a non-array hits (e.g. null, an object, a string) would otherwise cause a runtime TypeError on .map() — this throw gives it a descriptive message.

Source

Thrown at lib/routes/skeb/search.ts:68

                        filters: 'genres:art OR genres:comic OR genres:voice OR genres:novel OR genres:video OR genres:music OR genres:correction',
                    },
                    {
                        indexName: 'Request',
                        query: keyword,
                        params: 'hitsPerPage=40&filters=genre%3Aart%20OR%20genre%3Acomic%20OR%20genre%3Avoice%20OR%20genre%3Anovel%20OR%20genre%3Avideo%20OR%20genre%3Amusic%20OR%20genre%3Acorrection',
                    },
                ],
            },
        });

        if (!data || !data.results || !Array.isArray(data.results) || data.results.length < 2) {
            throw new Error('Invalid data received from API');
        }

        const works = data.results[1].hits;

        if (!Array.isArray(works)) {
            throw new TypeError('Invalid hits data received from API');
        }

        return works.map((item) => processWork(item)).filter(Boolean);
    });

    return {
        title: `Skeb - Search Results for "${keyword}"`,
        link: `${baseUrl}/search?q=${encodeURIComponent(keyword)}`,
        item: items as DataItem[],
    };
}

View on GitHub (pinned to bed535e087)

Solutions

  1. Log the full data.results[1] object to inspect its actual shape when the error occurs.
  2. If hits can legitimately be absent, add a fallback: `const works = Array.isArray(data.results[1].hits) ? data.results[1].hits : [];` instead of throwing.
  3. If the Algolia response schema changed, update the data-extraction path to match the new structure.

Example fix

// before
const works = data.results[1].hits;
if (!Array.isArray(works)) {
    throw new TypeError('Invalid hits data received from API');
}

// after
const works = Array.isArray(data.results[1]?.hits) ? data.results[1].hits : [];
return works.map((item) => processWork(item)).filter(Boolean);
Defensive patterns

Strategy: type-guard

Validate before calling

const worksResult = data.results[1];
if (!worksResult || !Array.isArray(worksResult.hits)) {
    // return empty array instead of throwing for a non-critical missing field
    return [];
}

Type guard

function isHitsArray(val: unknown): val is unknown[] {
    return Array.isArray(val);
}

Prevention

When it happens

Trigger: The Algolia 'Request' index returns a result entry whose `hits` field is null or missing. This can happen when the index exists but returned zero hits and Algolia omits the hits array, or when the response schema changed to nest hits under a different key.

Common situations: Edge case where the search keyword returns matches in the 'User' index but the 'Request' index returns an empty/degenerate result object; or an Algolia API version change altered the result envelope.

Related errors


AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12). Data as JSON: /api/errors/1bbef0f657acf713. Report an issue: GitHub.