4

I'm getting the below error- when I type into a lightning:input whose value attribute is bound to a field on the custom object- when I don't use the default attribute. Is this because the default attribute is required for custom objects?

This works

<aura:attribute name="customObject" type="CustomObject__c"
                default="{'sobjectType': 'CustomObject__c'}"/>

<lightning:input  label="First Name" required="true"
                  value="{!v.customObject.FirstName__c}"/>

This errors out

<aura:attribute name="customObject" type="CustomObject__c"/>

<lightning:input  label="First Name" required="true"
                  value="{!v.customObject.FirstName__c}"/>

error

Research

I don't see anywhere in the docs mentioning that this is required. Rather it seems it is intended for default values, see here.

Furthermore this Trail Head example seems to clearly not need it for a custom object.

trail head example

  • How are you using customObject? Are you creating an instance in the JS or you are receiving value after server call? – Jayant Das Feb 14 at 15:50
  • Creating an instance in JS – shmuels Feb 14 at 15:51
2

Based on your comments:

Creating an instance in JS

Defaulting the attribute makes sure that you have an instance created for the object. If you don't have it defaulted, and if you attempt to utilize it in JS or the component, you will get a null instance. And thus, if you attempt to utilize a null instance further, you will get an error.


So, if you want to utilize an SObject instance in your component or JS, then you have to use default the sobjectType during declaration to create an instance of the object, and then utilize it in your component or JS to populate other fields or use for further operations.

<aura:attribute name="customObject" type="CustomObject__c"
                default="{'sobjectType': 'CustomObject__c'}"/>

OR

If you don't provide default, you will need to make sure you construct the object in the JS on initialization and provide the sobjectType attribute and utilize it accordingly.

var c = {'sobjectType' : 'CustomObject__c', 'Field1' : field1Value}; 
  • Thank you this makes perfect sense. Although from the TrailHead it seems that you can utilize it without creating an instance. See image above for Expense__c – shmuels Feb 14 at 18:28
  • Note this from trailhead -- Here’s a stripped down look at the expenseItem component, which we’ll fill in later. So that's only for reference as how you can declare an attribute. It does not really uses it in any JS context to populate or instantiate the attribute. – Jayant Das Feb 14 at 19:20
  • I see now that I didn't really understand your question above, my apologies. I'm not creating an instance in the JS, nor am I receiving a value after server call. The error is occurring well before that, when I start typing into the input whose value attribute is bound to a field on the custom object (the error is occurring before the code hits any JS controller or helper code). I updated my question to reflect this above. – shmuels Feb 15 at 1:09
  • @shmuels The answer still remains the same. You need to default to be able to create an instance before you could actually use it. I updated a bit of the verbiage. – Jayant Das Feb 15 at 1:23
  • Thank you. I wish this was in the docs; it's pretty crucial that you must instantiate the attribute before using it. – shmuels Feb 15 at 2:03
1

The new LWC components typically check their configuration in an afterRender or connectedCallback function, and can throw errors if there's missing data. The two general solutions are to either hide the components by default (e.g. <aura:if isTrue="{!not(empty(v.attr))}">), or provide a default value. Not all components follow this rule though, so it's not in the documentation only because it is not a universal truth; many LWC components are more lenient and will allow you to set the data later.

  • In this specific scenario, it's a null vs. an empty instance which possibly could cause issue if you attempt to access null instance elsewhere in the JS. Defaulting ensures you are dealing with an empty instance. – Jayant Das Feb 14 at 16:13
  • I'm not using LWC, does Aura work the same way? I should've mentioned that I'm using lightning:input; is this one of the components which work this way? I think it should probably still be in the docs where applicable. – shmuels Feb 14 at 18:26
  • @shmuels LWC is the underlying component for many standard components, even in Aura. I do agree it would be nice to have a list of components and attributes that can throw exceptions. – sfdcfox Feb 14 at 19:32

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.