• Ankita Vashisth
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
  Hi, Iam stuck in trailhead challenge in "Connect components with Events". I am getting above error(The campingList component isn't handing the added item correctly.), though my functionality is perfecly working fine. Please help me in this...I have tried solutions for the same error in this forum before, but they are not working as well.

CampingList.cmp:
 
<aura:component controller="CampingListController">
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    
    <aura:handler name="addItem" action="{!c.handleAddItem}" event="c:addItemEvent"/>
    
    <aura:attribute name="items" type="Camping_Item__c[]" />
    
    <c:campingHeader />
    
    <c:campingListForm />
    
    <aura:iteration items="{!v.items}" var="item">
        <c:campingListItem item="{!item}" />
    </aura:iteration>
    
</aura:component>

CampingListController.js:
 
({

    doInit: function(component, event, helper){
        
        var action=component.get("c.getItems");
        action.setCallback(this, function(response)
                           {
                               var state = response.getState();
                               if(component.isValid() && state=='SUCCESS')
                               {
                                   component.set("v.items", response.getReturnValue());
                               }
                               else
                               {
                                   console.log("Error in state: "+ state);
                               }
                           });

        $A.enqueueAction(action);
        
    },
    
    handleAddItem : function(component, event, helper)
    {
        var item=event.getParam("item");
        var action=component.get("c.saveItem");
        
        action.setParams
        ({
                "item" : item
        });
        
        action.setCallback(this, function(response)
                           {
                               var state=response.getState();
                               if(component.isValid() && state == 'SUCCESS')
                               {
                                   var itemArray=component.get("v.items");
                                   itemArray.push(response.getReturnValue());
                                   component.set("v.items", itemArray);
                               }
                               else
                               {
                                   console.log('Error in state: '+ state);
                               }
                           });
        
        $A.enqueueAction(action);
    },
})