[Vuejs]-Vue.js "Cannot read property … of undefined" – Getting around this expected behavior

4👍

This doesn’t really have anything to do with Vue. You’re dealing with a javascript error here. You can’t reference a property of a null/undefined variable. The easy way around this is to use the && operator to check for the object.

someObj && someObj.property

If someObj is falsy, the right side is never evaluated. Note that when the left side is truthy, && returns the right side. Details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

If you want a specific blank value, you can do this

someObj ? someObj.property : "Default value"
👤James

Leave a comment