0

I have an attribute of type CustomSetting__c(which is a list type of CUSTOM SETTING) and I want to iterate over it to fetch its each record, and print that record's certain fields. I am unable to iterate over it and '.length' property is givng me error as:

[Cannot read property 'length' of undefined]

Please find related code:

Lightning component attribute:

<aura:attribute name="customSettingList" type="CustomSetting__c[]" />

On click of certain button, I want to go to helper of this component and execute this code:

Helper.js:

//some code before this
component.set("v.customSettingList", response.getReturnValue());
//some code after this statement

    var customSettingList=[];
    customSettingList=component.get("v.customSettingList");
    for(var i=0;i<customSettingList.length;i++){
        console.log('customSettingList type: '+ customSettingList[i].Type__c);
    }  

where Type__c is a custom field of this custom setting.

What is the reason for this? How can I access the field values of individual records from list?

Note: This is a list type of custom setting if that has to do anything with this.(Can we not access list custom setting in JS?)

2

The issue you are facing here is the fact that your customSettingList is undefined. This means you have not assigned any values to it.

This being said I'm missing the part where you are actually fetching the custom settings list.

Simply declaring this on your component is not sufficient:

<aura:attribute name="customSettingList" type="CustomSetting__c[]" />

The above code is simply telling lightning/aura that your component has an attribute of type CustomSetting__c[]. By default this attribute has no values assigned, nor is lightning capable of doing this for you. You will need to access the server yourself in order to get the list of custom settings and assign it to your attribute. You can do this by adding an Apex controller to your component.

More details can be found here: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_intro.htm

  • I am populating this attribute by values fetched from server. Please find updated code. I am getting values in response.getReturnValue(). – Sarang Dec 12 '16 at 12:39
  • I see it says response.getRetuenValue(), typo in the example? – Lieven Juwet Dec 12 '16 at 12:49
  • 1
    my bad..it did typo here. Corrected the code – Sarang Dec 12 '16 at 12:50
  • First thing I would do is check the value you get returned when calling the server. See if something is in there or if it's already undefined when you get the response from the server. If it's undefined you'll need to debug your controller. If it has values it might be that the value is set empty somewhere else in your JS code. – Lieven Juwet Dec 12 '16 at 12:52
  • When I print it immedietly after getting from server, it shows me values. However, once I set it in attribute, and later fetch it using component.get(), it is showing undefined. Is it due to some problem in definition of my attribute? – Sarang Dec 12 '16 at 13:01
0

I think I got it. It was because the part where I set the array using set() was in callback and the code where I was printing using component.get() was called before this callback, though it was after it in the flow. Since it was callback, the value was not set till the time I was trying to access it

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.