5

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
  }

this already gives me the following: testNotExistingAttribute

3

component.get("Attr") generally gives undefined if the attribute is not present. It doesn't throw an exception.

In JavaScript, the standard way to catch null and undefined simultaneously is this:

if (component.get("Attr")) {
    console.log("Happy");
}else{
    console.log("Sad");
}
  • 2
    Really, that's not necessary. Both undefined and null are falsy values, so if(component.get("Attr")) would be the same as what you wrote. – sfdcfox Jun 6 '17 at 14:25
  • Thank you for your thoughts - I have updated my question because what you proposed is exactly what I struggle with (maybe a Summer17 behavior-change) – Christian Menzinger Jun 6 '17 at 14:40
  • @sfdcfox Thanks for your suggestion, I updated my answer. – Saroj Bera Jun 7 '17 at 6:54
  • @Menzman Maybe this is due to locker service, I will update the answer if I find something. – Saroj Bera Jun 7 '17 at 6:56
3

Thanks for validating that not everybody gets the exception- so I found out that when Debug Mode is enabled (Setup -> Lightning Components -> Checkbox) the exception is raised to the user interface.

Does this mean checking an attribute for existence by using if (component.get("Attr")) is safe in production?

  • Thanks for posting this update. Very interesting - I've never used Debug Mode (didn't even know about it until now), so I'll have to play around with it. Looks like Debug Mode might catch and warn about exceptions that non-debug-mode doesn't care about? Interesting... – Florissant53 Jun 8 '17 at 15:19
  • I was able to reproduce the exception by enabling Debug Mode. According to this article I found, when Debug Mode is disabled, the Google Closure Compiler is used to minimize and optimize the code for production use. My guess is when Debug Mode is enabled, the code isn't run through this process, it's left "as is", and certain exceptions (such as undefined attributes) are raised instead of being handled silently. Article at: help.salesforce.com/… – Florissant53 Jun 8 '17 at 16:37
  • Note that article I posted says that Debug Mode incurs a significant performance impact for all users in the org, so it should be left disabled in production orgs, and only used for debugging in a sandbox or Developer Edition org. – Florissant53 Jun 8 '17 at 16:39
  • Yes, debug mode is a develop-only setting and not meant to use in production. But it helps you debugging your code. Only strange thing I experienced so far is this exception issue... – Christian Menzinger Jun 9 '17 at 9:23
  • I wonder if this specific exception is thrown in debug mode to help capture where your code is looking for an attribute that wasn't found so you can assert you didn't do a typo or incorrect logic. Dunno... but that rationalization helps me sleep at night. – Doug Ayers Aug 19 '17 at 19:32
1

Another way of doing it is by using the util methods, such as:

var someAttribute = component.get('v.someAttribute');

if ($A.util.isUndefinedOrNull(someAttribute)) {
    // Do something when undefined or null
}
else {
    // Do something if defined and has a value
}

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.

  • Thanks to mention util methods, but this is not solving the issue as the exception would be already raised in the first line of your example. – Christian Menzinger Jun 7 '17 at 7:07
  • No exception is raised when doing component.get against an undefined attribute. I just ran this: "var undefinedAttribute = component.get('v.undefinedAttribute');" and verified no exception is raised. The variable is just left undefined. Lastly, this behavior is WITH Locker Service enabled (I only develop with it enabled). – Florissant53 Jun 7 '17 at 18:28
  • Just to be sure this isn't impacting by having it in an init method, I put the same code in a component init handler, and still don't get an exception. One difference could be that my app is a standalone Lightning app, and doesn't run within Lightning Experience or Salesforce1. Is your component running in one of those environments? If so, maybe those are more strict regarding undefined attributes being retrieved with component.get(). – Florissant53 Jun 7 '17 at 18:53
  • this is very interesting: on my DE org an exception is raised also on a standalone app. – Christian Menzinger Jun 8 '17 at 10:07
  • I have an old DE org I haven't logged into for a long time. I'll try this later today in there and see if I also get an exception, and report the findings. EDIT - Nevermind, I just saw your additional answer/update below re: debug mode. – Florissant53 Jun 8 '17 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.