How can I check whether a certain attribute is existing within my component?
Especially when I try to access a value dynamically like component.get("v." + fieldName)
I want to make sure this is not throwing an exception. Unfortunately try/catch
is not catching the frameworks default "Access Check failed"-exception nor I haven't found any suitable function in $A.util or the Component-API.
I am not 100% sure if this behavior changed with Summer17 but when I do a simple test on a component init handler:
onInit: function(component, event, helper) {
if(component.get("v.testNotExistingAttribute")) {
//never reach this code as exception is thrown
}
Best Answer
Another way of doing it is by using the util methods, such as:
As sfdcfox wrote, undefined and null are falsy values and can be tested as such, but I prefer using the util methods because for documentation purposes for myself, it's a little more descriptive to me what I'm testing for.
Anyway, thought I'd toss the util method out there as an alternate way of checking for attributes.