You need to sign in to do that
Don't have an account?
WonJeung
Error while doing Aura Components Basics module on Trailhead
Hello, I'm new to Salesforce and I'm currently learning Aura Components Basics Module. I'm having problems with Input Data Using Forms unit at the moment.
The error says : "The campingList component isn't iterating the array of 'items' and creating 'campingListItem' components"
but it seems my program works fine, and I don't understand what exactly the error means?
Here's my code:
campingApp.app
campingList.cmp
campingListController.js
campingListItem.cmp
It would be great if someone explains why the error message says the campingListItem isn't created. Thanks!
The error says : "The campingList component isn't iterating the array of 'items' and creating 'campingListItem' components"
but it seems my program works fine, and I don't understand what exactly the error means?
Here's my code:
campingApp.app
<aura:application extends = "force:slds" > <c:campingHeader/> <c:campingList/> </aura:application>
campingList.cmp
<aura:component > <aura:attribute name = "items" type = "Camping_Item__c[]"/> <aura:attribute name = "newItem" type = "Camping_Item__c" default = "{ 'sobjectType': 'Camping_Item__c', 'Quantity': 0, 'Price': 0}"/> <!-- NEW CAMPING FORM STARTS HERE --> <form class="slds-form--stacked"> <lightning:input aura:id = "campingform" label = "Name" value = "{!v.newItem.Name__c}"/> <lightning:input aura:id = "campingform" type = "number" label = "Quantity" min = "1" value = "{!v.newItem.Quantity__c}"/> <lightning:input aura:id = "campingform" type = "number" label = "Price" formatter = "currency" value = "{!v.newItem.Price__c}"/> <br></br> <lightning:input aura:id = "campingform" type = "checkbox" label = "Packed?" value = "{!v.newItem.Packed__c}"/> <br></br> <lightning:button label = "Add an item" onclick = "{!c.clickCreateItem}"/> </form> <!-- CAMPING FORM ENDS HERE --> <!-- LIST OF CAMPING ITEMS STARTS HERE --> <aura:iteration items = "{!v.items}" var = "item"> <c:campingListItem campingListItem = "{!item}"/> </aura:iteration> <!-- END OF CAMPING ITEMS --> </aura:component>
campingListController.js
({ clickCreateItem : function(component, event, helper) { var validItem = component.find('campingform').reduce(function(validSoFar, inputCmp){ inputCmp.showHelpMessageIfInvalid(); return validSoFar && inputCmp.get('v.validity').valid; }, true); if(validItem){ var newItemList = component.get("v.items"); var newItem = JSON.parse(JSON.stringify(component.get("v.newItem"))); console.log("===== BEFORE ====="); console.log(JSON.stringify(newItemList)); newItemList.push(newItem); component.set("v.items", newItemList); console.log("===== AFTER ====="); console.log(JSON.stringify(newItemList)); } } })
campingListItem.cmp
<aura:component> <aura:attribute name="campingListItem" type="Camping_Item__c"/> <p>{!v.campingListItem.Name__c}</p> <p>{!v.campingListItem.Quantity__c}</p> <p>{!v.campingListItem.Price__c}</p> <p>{!v.campingListItem.Packed__c}</p> </aura:component>
It would be great if someone explains why the error message says the campingListItem isn't created. Thanks!
I think you have problem in your campinglistItem component, I'm sharing the codes of campingListItem and campingList, refer to them.
campingList:
COMPONENT:
<aura:component controller="CampingListController">
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<aura:handler name="addItem" event="c:addItemEvent"
action="{!c.handleAddItem }"/>
<aura:attribute name="items" type="Camping_Item__c[]"/>
<ol>
<li>Bug Spray</li>
<li>Bear Repellant</li>
<li>Goat Food</li>
</ol>
<!-- NEW ITEM FORM -->
<div class="slds-col slds-col--padded slds-p-top--large">
<c:campingListForm />
</div>
<!-- / NEW ITEM FORM -->
<div class="slds-card slds-p-top--medium">
<header class="slds-card__header">
<h3 class="slds-text-heading--small">Items</h3>
</header>
<section class="slds-card__body">
<div id="list" class="row">
<aura:iteration items="{!v.items}" var="items">
<c:campingListItem item="{!item}"/>
</aura:iteration>
</div>
</section>
</div>
</aura:component>
CONTROLLER:
campingListItem:
COMPONENT:
CONTROLLER:
Hoping these two code samples will solve your problem. Please let me know about the outcomes.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi