feat: update getQuizResult to handle no records gracefully by using getList
This commit is contained in:
@@ -194,9 +194,16 @@ export async function saveQuizResult(userId, weekNumber, result) {
|
||||
|
||||
export async function getQuizResult(userId, weekNumber) {
|
||||
try {
|
||||
const r = await pb.collection('test_results').getFirstListItem(
|
||||
`user_id="${userId}" && week_number=${weekNumber}`,
|
||||
);
|
||||
// Use getList instead of getFirstListItem so that "no record yet" returns
|
||||
// an empty items array (HTTP 200) rather than throwing a ClientResponseError
|
||||
// that the SDK logs to the browser console as a 404.
|
||||
const res = await pb.collection('test_results').getList(1, 1, {
|
||||
filter: `user_id="${userId}" && week_number=${weekNumber}`,
|
||||
skipTotal: true,
|
||||
requestKey: null,
|
||||
});
|
||||
const r = res.items[0];
|
||||
if (!r) return null;
|
||||
// Map snake_case PB fields back to the camelCase shape Testen.jsx expects.
|
||||
return {
|
||||
score: r.score,
|
||||
|
||||
Reference in New Issue
Block a user