1

i have the following line:

var childCmp = cmp.find("lookupCmp");

My Lightning component API Version is 44.0

lookupCmp is a custom lightning component.

When i run this line i get:

childCmp = Proxy {0: a, 1: a, 2: a, length: 3}

enter image description here

i want to access the elements inside target and im not sure how to do this, and couldn't find an answer

i tried to follow this document https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_dom.htm and use

cmp.find("lookupCmp").getElement()

and received the error

"getElement is not a function"

4
  • You can pass child component values with lightning events. developer.salesforce.com/docs/atlas.en-us.lightning.meta/… check this link Commented Dec 24, 2018 at 11:31
  • i dont understand how its related to the question. also, in the next part of the code im accessing its DOM elements, so getting only the value wont help me
    – Dana Griff
    Commented Dec 24, 2018 at 11:35
  • By values I meant variables not component attributes. If you can edit child component (lookupCmp) you can also pass dom elements in it with events. For example: You can get element value with "var divElements = cmp.find('elementId').getElements();" in child component and pass it to parent with an event using attribute type object. But this also means you need to trigger event from the child component. This really depends on your requirements. I can be more helpful if you can explain what you want to achieve by accessing DOM elements in other components. Commented Dec 24, 2018 at 12:36
  • 1
    Just as a side note, reaching into another component's DOM (even when it's allowed in Lightning) can make it harder to work on your application later. It establishes a tight coupling between the parent and child and breaks the child's encapsulation. Often, it's better in the long term to use component methods or events to communicate.
    – David Reed
    Commented Dec 24, 2018 at 20:05

2 Answers 2

2

Proxy is simply an object that has custom get/set methods, which allows Locker Service to restrict access to private attributes. You still need to check if the result is an array, then you can access the individual elements if you desire.

let childCmp = cmp.find("lookupCmp");
if(!childCmp) {
  // Nothing to do
} else if(!childCmp.length) {
  let child = childCmp.getElement(); // just one
  // ...
} else {
  childCmp.forEach(item => {
    let child = item.getElement();
  }
}

Note that getElement will only return a value if the element is in the same namespace as your component.

2
  • didnt work unfortunately
    – Dana Griff
    Commented Dec 30, 2018 at 14:51
  • @DanaGriff Then you don't have access to those components element/elements. All you have access to will be the set/get methods of the component and any aura:method methods defined on the component.
    – sfdcfox
    Commented Dec 30, 2018 at 15:23
2

This article helped me and worked: Getting a Proxy object instead of an actual component attribute

i parsed the component as a json like described:

const objUncovered = JSON.parse(JSON.stringify(objWhichIsCoveredWithProxy));

You must log in to answer this question.

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