Skip to content

Commit 1c85849

Browse files
fix: handle errors when fetching latest questions and filter out undefined results
1 parent 956b463 commit 1c85849

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/app/components/LatestQuestions.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const LatestQuestions = async () => {
1313
]);
1414
console.log("Fetched Questions:", questions);
1515

16-
questions.documents = await Promise.all(
16+
const mappedQuestions = await Promise.all(
1717
questions.documents.map(async ques => {
18-
const [author, answers, votes] = await Promise.all([
18+
try {
19+
const [author, answers, votes] = await Promise.all([
1920
users.get<UserPrefs>(ques.authorId),
2021
databases.listDocuments(db, answerCollection, [
2122
Query.equal("questionId", ques.$id),
@@ -27,7 +28,6 @@ const LatestQuestions = async () => {
2728
Query.limit(1), // for optimization
2829
]),
2930
]);
30-
3131
return {
3232
...ques,
3333
totalAnswers: answers.total,
@@ -38,8 +38,14 @@ const LatestQuestions = async () => {
3838
name: author.name,
3939
},
4040
};
41+
} catch (error) {
42+
console.log("Error fetching lastest questions", error)
43+
return undefined;
44+
}
4145
})
4246
);
47+
questions.documents = mappedQuestions.filter((q): q is NonNullable<typeof q> => q !== undefined);
48+
console.log("Mapped:",questions.documents)
4349

4450
return (
4551
<div className="space-y-6">

0 commit comments

Comments
 (0)