--- title: Check for Null/Undefined Before Accessing Properties in v-if impact: MEDIUM impactDescription: Accessing properties on null/undefined causes runtime errors and crashes type: capability tags: [vue3, conditional-rendering, v-if, null-check, defensive-programming] --- # Check for Null/Undefined Before Accessing Properties in v-if **Impact: MEDIUM** - Accessing properties on null or undefined objects in `v-if` conditions causes "Cannot read property of undefined" runtime errors. This commonly occurs when data is loaded asynchronously or when optional object properties are accessed without null checks. ## Task Checklist - [ ] Always check that an object exists before accessing its properties - [ ] Use optional chaining (?.) in Vue 3 templates for cleaner null checks - [ ] Consider using computed properties for complex conditional logic - [ ] Handle loading states explicitly rather than relying on undefined checks **Incorrect:** ```html ``` ```html ``` ```html ``` **Correct:** ```html ``` ```html ``` ```html ``` ```html ``` ```html ``` ## Using Computed Properties for Complex Checks ```javascript // CORRECT: Move complex checks to computed properties ``` ## Common Async Data Pattern ```javascript // CORRECT: Handle async data loading properly ``` ## Reference - [Vue.js Conditional Rendering](https://vuejs.org/guide/essentials/conditional.html) - [MDN - Optional Chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)