Pagination offset calculation
Review this TypeScript pagination endpoint.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 25 min
Mark a line and say what kind of problem it is.0 findings
1// GET /orders?page=1&limit=20
2router.get('/orders', async (req, res) => {
3 const page = Number(req.query.page) || 1;
4 const limit = Number(req.query.limit) || 20;
5 const offset = (page - 1) * limit;
6
7 const rows = await db.query(
8 'SELECT * FROM orders WHERE user_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3',
9 [req.user.id, limit, offset]
10 );
11
12 res.json({ orders: rows, page, limit });
13});
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.