Accessing List in Lightning components

lightning-aura-components

I generated a list earlier on in the code and stored it.

<aura:attribute name="voiceContactList" type="List"/>
contactList: Proxy
    [[Handler]]: Object
        [[Target]]: Array(1)
            0: {firstName: 'Ktest2', firstName2: 'Ktest3', LastName: 'Test2', LastName2: 'Test3', streetAddress: '911 SomeTrail Trail', …}
            length: 1 [[Prototype]]: Array(0) [[IsRevoked]]: false

The list is there in contactList but I haven't been able to access this list.

console.log('contact = '+ contactList);

Just shows it as [object,object]

Best Answer

The strange syntax you see is because the Lightning LockerService has wrapped your object in a Proxy. This makes it inaccessible to anything other than your own javascript context.

To access in the console, you can JSON stringify:

console.log('contact = '+ JSON.stringify(component.get("v.contactList"))); 

Also see note, to access the attribute, you must use the component getter syntax component.get("v.yourvarname").

To turn into a javascript variable in your controller or helper, do this:

let contacts = component.get("v.contactList");

If you examined this in the console you would see this is still a Proxy object, but if you attempted to access a property, it would be available, ie:

let fname = contacts[0].firstName;

Would work fine.

Related Topic