# PocketBase Query Pitfalls (spq-v2) These recurring bugs cause silent page crashes and "Something went wrong" in spq-v2 React pages. Check these FIRST when a page breaks. ## sort: '-created' fails on base collections PB auto-adds `created`/`updated` to **auth** collections only, not **base** collections. spq-v2 uses base collections (quotes, repairOrders, appointments, customers, services, settings). Always use `sort: '-id'` — PB IDs are ULID-based and time-sortable. **Symptom**: Page shows "Something went wrong". **Fix**: `pb.collection('repairOrders').getList(1, 200, { sort: '-id' })` ## JSON fields come as strings from PB API When PocketBase stores a JSON field, the API may return it as a **serialized string**, not a parsed array/object. Accessing `.reduce()` or `.map()` on a string throws. **Symptom**: `e.reduce is not a function`, `Unexpected end of JSON input`. **Fix** — always normalize: ```ts let services = item.services || []; if (typeof services === 'string' && services.trim()) { try { services = JSON.parse(services); } catch { services = []; } } if (!Array.isArray(services)) services = []; ``` ## `fields` parameter causes silent failures Restricting fields with `fields: 'id,customerName,...'` fails silently if any field name doesn't exist in the collection. PB returns an error that is hard to diagnose. **Symptom**: "Failed to load appointments" with cryptic message. **Fix**: Omit `fields` entirely and let PB return all fields. The bandwidth cost is negligible. ## Date formatting: always guard against undefined `new Date(undefined)` creates an Invalid Date object. Calling `.toLocaleDateString()`, `.toISOString()`, or any format method on it throws `RangeError: Invalid time value`. **Symptom**: White screen, `Uncaught RangeError: Invalid time value`. **Fix** — always add null guards: ```ts function formatDate(iso: string | undefined | null) { if (!iso) return '—'; const d = new Date(iso); if (isNaN(d.getTime())) return '—'; return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }); } ``` # React/Vite Pitfalls (spq-v2) ## Input focus loss in nested components Defining a component as a nested function inside another component causes React to destroy/recreate it on every parent re-render. Input fields lose focus on every keystroke. **Symptom**: Typing in a price/textarea field causes focus loss after each character. **Fix**: Extract to file-level and wrap with `React.memo`: ```tsx const ServiceRow = memo(function ServiceRow(props: ServiceRowProps) { return