1
votes

How do you get child node content with JavaScript specifically?

I am able to get the parent node information by using the following:

granite.resource.properties

However I need to have access to a child node

-parent

-- child (named image)

There are ways to get access to the child node with Java but I am building a JavaScript only solution:

Java Example below

for (Node n1 : JcrUtils.getChildNodes(node)){
  String imagePath = n1.getPath().toString();
  Resource imageResource = resourceResolver.getResource(imagePath);
  Node imageNode = imageResource.adaptTo(Node.class);
  ValueMap imageNodeProps = imageResource.adaptTo(ValueMap.class);
  String imageName = null;
  imageName= imageNodeProps.get("fileReference", "None");
3

3 Answers

0
votes

I don't know how to get the child node properties but maybe I had similar problem because I wanted to use an image in the component. I tried to set the image as child node, that didn't work because I didn't know how to access it. What I ended up is loading the image using css.

0
votes

Using the image base component that comes with AEM helps get guidance on how to access te data. For me it was just a matter to make some modifications and have it working as expected. You can find the AEM image component here:

/libs/wcm/foundation/components/image/

Browse through the different files provided to get more info.

0
votes

You may use console.log() in between the code and check the values in the error.log

function(nodeInfo) {
    /* from current node, get the children nodes */
    nodeInfo.childrenNodes = currentNode.getNodes();
    if(typeof nodeInfo.childrenNodes != "undefined") {

        for(var childrenNode in nodeInfo.childrenNodes) {
             /*iterate through children nodes, get specific 'child' node */
             if(childrenNode === "specificChildNode") {
                 /* if required property is present, get that property */
                 if(nodeInfo.childrenNodes[childrenNode].hasProperty("requiredProperty")){
                    nodeInfo.reqProp = nodeInfo.childrenNodes[childrenNode].getProperty("requiredProperty");
                 } 
             }
         }
    }
    return nodeInfo;
};