KomaSattarov
KomaSattarov15mo ago

Constantly getting this error and cant get pass by

const topics = useQuery(api.titles.collectTitles); const filteredTopics = useMemo( () => Object.entries(topics).reduce((acc, [key, topic]) => { if ( fields.categories.length > 0 && (!topic.category || !fields.categories.includes(topic.category)) ) { return acc; } if ( fields.questionTypes.length > 0 && !fields.questionTypes.includes(topic.questionType as string) ) { return acc; } if ( fields.search && !topic.name.toLowerCase().includes(fields.search.toLowerCase()) && !topic.questionDesc ?.toString() .toLowerCase() .includes(fields.search.toLowerCase()) ) { return acc; } acc?.push(topic); return acc; }, {} as typeof topics), [fields, topics] ); Type 'undefined' is not assignable to type '{}'.ts(2769) This error is showing up here when the topics is declared Object.entries(topics).reduce
5 Replies
ballingt
ballingt15mo ago
It looks like this is TypeScript helping out appropriately: while the query is loading, topics will indeed be undefined! and Object.entries(undefined) throws an error You could use a conditional if (topics === undefined) return <Loading />; or use a default value like const topics = useQuery(api.titles.collectTitles) || []
jamwt
jamwt15mo ago
@ballingt is ?? more idiomatic these days, or am I off?
ballingt
ballingt15mo ago
yeah you cool kids, you can do that too
jamwt
jamwt15mo ago
just in case the return value is "falsy" but still not undefined and therefore query-provided
ballingt
ballingt15mo ago
e.g. "" or 0

Did you find this page helpful?