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) {
|
export async function getQuizResult(userId, weekNumber) {
|
||||||
try {
|
try {
|
||||||
const r = await pb.collection('test_results').getFirstListItem(
|
// Use getList instead of getFirstListItem so that "no record yet" returns
|
||||||
`user_id="${userId}" && week_number=${weekNumber}`,
|
// 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.
|
// Map snake_case PB fields back to the camelCase shape Testen.jsx expects.
|
||||||
return {
|
return {
|
||||||
score: r.score,
|
score: r.score,
|
||||||
|
|||||||
Reference in New Issue
Block a user