{"success":true,"name":"expect-populate-to-return-null-every-reference-can-dangle-so","description":"Using Mongoose populate/$lookup and rendering the joined data directly","category":"ai-brimind","path":"ai-brimind/expect-populate-to-return-null-every-reference-can-dangle-so/SKILL.md","file":"SKILL.md","content":"---\nname: expect-populate-to-return-null-every-reference-can-dangle-so\ndescription: \"Using Mongoose populate/$lookup and rendering the joined data directly\"\n---\n\n# Expect populate() to Return Null: Every Reference Can Dangle, So Every Join Needs a Fallback\n\n## When to use\nCode populates a reference (member.club, order.user) and immediately reads its properties, assuming the referenced document always exists.\n\n## Example request\nmember.club.name crashes with 'cannot read properties of null' → the club was deleted last month but members still point at it; populate found nothing and returned null, exactly as documented — the missing piece was your branch for it. The screen shows blank rows wherever the populated user is null → deleted authors turned every list into Swiss cheese; a fallback label ('Deleted user') and a filter decision beat a crash, but the decision must be MADE. Dangling references exist because Mongo does not enforce foreign keys → your delete-design discipline prevents NEW orphans, but old data and race conditions still produce them; the read side must stay defensive regardless. A reference guaranteed by a transaction created in the same write → still guard the render; guarantees rot, null checks don't.\n\n## Instructions\nAct as a developer who reads every populate() and $lookup with one truth in mind: MongoDB does not enforce references — a stored clubId is a claim, not a guarantee — so the referenced document may have been deleted, never created, or excluded by tenant scoping, and populate will faithfully return null (or $lookup an empty array) rather than error; the crash comes one line later, in YOUR code, when it dereferences the ghost. For every join you write, do this: (1) At the moment you write .populate('club'), write the null branch in the same breath: what does this code path do when club is null? The three honest options are — render a fallback (a 'Deleted club' label, per your loading/error/empty discipline this is a fourth face of data: 'referenced but gone'), filter such records out of the list (a deliberate query or post-filter, stated in a comment), or treat it as data corruption worth logging (per your log-with-intent habits: WHICH member points at WHICH missing club, so the orphan is findable and fixable). Pick one per context; picking none is picking the crash. (2) Guard the access pattern itself: optional chaining (member.club?.name ?? 'Unknown') at render sites, and in aggregations remember $lookup yields an ARRAY — empty when unmatched — so the $unwind that follows silently DROPS unmatched records unless you set preserveNullAndEmptyArrays: true; that default has quietly deleted rows from a thousand dashboards, decide it consciously. (3) Connect the read-side defense to the write-side causes without confusing them: your delete-with-a-plan discipline (cascade/restrict/detach) prevents FUTURE orphans, and a cleanup/reconciliation script can heal OLD ones — but the render guard stays forever, because imports, manual edits, and races keep a trickle of dangling references alive in any real system; defense in depth, not either/or. (4) When populating chains (member.club.owner.name), each hop multiplies the nulls — guard every level or restructure the query to fetch what the screen truly needs per your response-shaping discipline. (5) Test with the ghost you know exists: create a record, delete its parent directly in the DB, load every screen that joins them — no crash, a sane fallback, and (if you chose logging) one clear log line. Only call the join done when a dangling reference produces a decision you made instead of an error you didn't — in a database without foreign keys, every populate is an optimistic question, and code that cannot hear 'no' for an answer was never asking, it was assuming.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #242\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}