1

I am creating lightning component dynamically. On callback, I am facing a strange error which should not come.

 $A.createComponent(
            "c:TestComponent",
            {
                parameter1 : "hey"
            },
            function(newComponent, status){
                    var con = component.find('container');
                    if (component.isValid() && con.isValid() && status === 'SUCCESS') {
                        var body = con.get("v.body");
                        body.push(newComponent);
                        con.set("v.body", body);
                    }                
            });

and .cmp file I have

<div aura:id="container" class="slds-align-top"> 
</div>

and I am getting error in if condition.

con.isValid is not a function

Since container itself is not valid, I am not able to read the body of container where I want to include newly created component? What is wrong?

0

1 Answer 1

2

As far as I can tell, component.find() is returning something other than exactly one value. Creating a Minimum Viable Reproduction, I wrote this code:

<aura:application extends="force:slds">
    <aura:handler name="init" value="{!this}" action="{!c.init}" />

    <div aura:id="container" class="slds-align-top"> 
    </div>

</aura:application>

({
    init: function(component, event, helper) {
        $A.createComponent(
            "c:test",
            null,
            function(newComponent, status){
                var con = component.find('container');
                if (component.isValid() && con.isValid() && status === 'SUCCESS') {
                    var body = con.get("v.body");
                    body.push(newComponent);
                    con.set("v.body", body);
                }                
            });     
    }
})

And the component was created correctly and appeared on the page. Make sure you have exactly one element named "container".

You must log in to answer this question.

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