0

It works when I test it but I'm getting the following Trailhead error:

The campingList JavaScript controller isn't calling 'saveItem' in the Apex class to save the record or setting the 'item' as a parameter for the 'saveItem' action.

Here is my code:

campingList.cmp

<aura:attribute name="items" type="Camping_Item__c[]" default="[]"/>

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<aura:handler name="addItemEvent" event="c:addItemEvent" action="{!c.handleAddItem}"/>


<c:campingListForm />

<aura:iteration items="{!v.items}" var="item">

    <c:campingListItem item="{!item}"/>

</aura:iteration>

campingListController.js

({
    doInit : function(component, event, helper){

        var action = component.get("c.getItems");
        action.setCallback(this, function(response) {
            var state = response.getState();
            console.log(response.getReturnValue());
            if (state === "SUCCESS") {
                component.set("v.items", response.getReturnValue());
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);

    },

    handleAddItem : function(component, event, helper){

        var action = component.get("c.saveItem");
        var item = event.getParam("item");
        //helper.createItem(component, item);

        action.setParam("item", item);

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log("Succeeded");

                //var newItem = component.get("v.newItem");
                var items = component.get("v.items");

                //component.set("v.items", response.getReturnValue());

                items.push(item);
                component.set("v.items", items);
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);

    }
})

campingListHeader.js

({

    createItem : function(component, event, helper){

        var item = event.getParam("item");
        console.log(item);

        var action = component.get("c.saveItem");
        action.setParam("item", item);

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log("Succeeded");

                //var newItem = component.get("v.newItem");
                //var items = component.get("v.items");

                component.set("v.items", response.getReturnValue());

                component.set("v.newItem", {
                                "SObjectType" : "Camping_Item__c",
                                'Price__c' : 0,
                                'Quantity__c' : 0
                            });



                //items.push(newItem);
                //component.set("v.items", items);
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
    }

})

campingListForm.cmp

<aura:attribute name="newItem" type="Camping_Item__c" default="{
    'SObjectType' : 'Camping_Item__c',
    'Price__c' : 0,
    'Quantity__c' : 0
}"/>

<aura:registerEvent name="addItemEvent" type="c:addItemEvent"/>

<lightning:input aura:id="inputField" label="Name" type="text" value="{!v.newItem.Name}" required="true"/>
<lightning:input aura:id="inputField" label="Price" type="number" value="{!v.newItem.Price__c}" required="true" step="1" formatter="currency"/>
<lightning:input aura:id="inputField" label="Quantity" type="number" value="{!v.newItem.Quantity__c}" required="true" step="1" min="1"/>
<lightning:input aura:id="inputField" label="Packed" type="checkbox" checked="{!v.newItem.Packed__c}"/>

<lightning:button label="Add" onclick="{!c.clickCreateItem}"/>

campingListFormController.js

({
    clickCreateItem : function(component){

        var isValid = component.find('inputField').reduce(function (validSoFar, inputCmp) {
            // Displays error messages for invalid fields
            inputCmp.showHelpMessageIfInvalid();
            return validSoFar && inputCmp.get('v.validity').valid;
        }, true);
        // If we pass error checking, do some real work
        if(isValid){

            var event = component.getEvent("addItemEvent");
            event.setParam("item", component.get("v.newItem"));

            event.fire();


            component.set("v.newItem", {
                "sobjectType" : "Camping_Item__c",
                'Price__c' : 0,
                'Quantity__c' : 0
            });
        }

    }
})
0

You're using action.setParam("item", item); but I think you need setParams like this action.setParams({"item", item});

  • Tried it. Still throws the same error. Thanks though! – Lachlan B Nov 15 '18 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.