6

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 Answers 3

4

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.

5
  • 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. Commented Jun 7, 2017 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). Commented Jun 7, 2017 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(). Commented Jun 7, 2017 at 18:53
  • this is very interesting: on my DE org an exception is raised also on a standalone app. Commented Jun 8, 2017 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. Commented Jun 8, 2017 at 15:13
4

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?

5
  • 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... Commented Jun 8, 2017 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/… Commented Jun 8, 2017 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. Commented Jun 8, 2017 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... Commented Jun 9, 2017 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
    Commented Aug 19, 2017 at 19:32
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");
}
4
  • 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
    Commented Jun 6, 2017 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) Commented Jun 6, 2017 at 14:40
  • @sfdcfox Thanks for your suggestion, I updated my answer.
    – Saroj Bera
    Commented Jun 7, 2017 at 6:54
  • @Menzman Maybe this is due to locker service, I will update the answer if I find something.
    – Saroj Bera
    Commented Jun 7, 2017 at 6:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .