function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vleandrovleandro 

Action failed: c$campingList$controller$createCampingList

I've searched the forums high and low and I'm still not getting something right with this.  

My form loads and loads my records from the database.  The problem is when I try and go to "Create Camping Item" I get this error:

This page has an error. You might just need to refresh it. Action failed: c$campingList$controller$createCampingList [component is not defined] Failing descriptor: {c$campingList$controller$createCampingList}

Here's the code:

campingList.cmp
<aura:component controller="CampingListController">
    
    <aura:attribute name="items" type="Camping_Item__c[]"/>
    
    <aura:attribute name="newItem" type="Camping_Item__c" default="{'Name':'',
                                                                   'Quantity__c':0,
                                                                   'Price__c':0,
                                                                   'Packed__c':false,
                                                                   'sobjectType':'Camping_Item__c'}"/>
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    
    <!-- NEW Campaing FORM -->
    <div class="slds-col slds-col--padded slds-p-top--large">
        
        
        <!-- [[ Campaing form goes here ]] -->
        
        <div aria-labelledby="newCampaingForm">
            
            <!-- BOXED AREA -->
            <fieldset class="slds-box slds-theme--default slds-container--small">
                
                <legend id="newCampaingForm" class="slds-text-heading--small 
                                                   slds-p-vertical--medium">
                    Add Expense
                </legend>
                
                <!-- CREATE NEW Campaing FORM -->
                <form class="slds-form--stacked">
                    
                    <div class="slds-form-element slds-is-required">
                        <div class="slds-form-element__control">
                            <ui:inputText aura:id="campingName" label="Camping Name"
                                          class="slds-input"
                                          labelClass="slds-form-element__label"
                                          value="{!v.newItem.Name}"
                                          required="true"/>
                        </div>
                    </div>
                    
                    <div class="slds-form-element slds-is-required">
                        <div class="slds-form-element__control">
                            <ui:inputNumber aura:id="quantity" label="Quantity"
                                            class="slds-input"
                                            labelClass="slds-form-element__label"
                                            value="{!v.newItem.Quantity__c}"
                                            required="true"/>
                            
                        </div>
                    </div>
                    
                    
                    
                    <div class="slds-form-element">
                        <div class="slds-form-element__control">
                            <ui:inputCurrency aura:id="price" label="Price"
                                              class="slds-input"
                                              labelClass="slds-form-element__label"
                                              value="{!v.newItem.Price__c}"/>
                        </div>
                    </div>
                    
                    <div class="slds-form-element">
                        <ui:inputCheckbox aura:id="packed" label="Packed ?"
                                          class="slds-checkbox"
                                          labelClass="slds-form-element__label"
                                          value="{!v.newItem.Packed__c}"/>
                    </div>
                    
                    <div class="slds-form-element">
                        <ui:button label="Create Camping Item"
                                   class="slds-button slds-button--brand"
                                   press="{!c.createCampingList}"/>
                    </div>
                    
                </form>
                <!-- / CREATE NEW EXPENSE FORM -->
                
            </fieldset>
            <!-- / BOXED AREA -->
            
        </div>
        <!-- / CREATE NEW EXPENSE -->
    </div>
    
    <!-- ITERATIING ITEM LISTS -->
    <div class="slds-card slds-p-top--medium">
        
        <c:campingHeader />
        
        <section class="slds-card__body">
            <div id="list" class="row">
                <aura:iteration items="{!v.items}" var="item">
                    <c:campingListItem item="{!item}"/>
                </aura:iteration>
            </div>
        </section>
    </div>	
    <!-- / ITERATIING ITEM LISTS -->
 
    
</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());
                 
            }
        });
        
        $A.enqueueAction(action);
	},
    
    createCampingList : function(component, event, helper){
    	// If we pass error checking, do some real work
        if(helper.validateCampingForm(component)) {
            var newItem = component.get("v.newItem");
            helper.createItem(component, newItem);
        }
    }
})

campingListHelper.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());
                 
            }
        });
        
        $A.enqueueAction(action);
	},
    
    createCampingList : function(component, event, helper){
    	// If we pass error checking, do some real work
        if(helper.validateCampingForm(component)) {
            var newItem = component.get("v.newItem");
            helper.createItem(component, newItem);
        }
    }
})

and last but not least, my Apex class, CampingListController.apxc
public with sharing class CampingListController {
    
    @AuraEnabled
    public static List<Camping_Item__c> getItems() {
        String[] fieldsToCheck = new String[] {
            'Id', 'Name', 'Quantity__c', 'Price__c', 'Packed__c', 'CreatedDate'};

        Map<String, Schema.SObjectField> fieldDescribeTokens = Schema.SObjectType.Camping_Item__c.fields.getMap();
        
        for(String field : fieldsToCheck) {
            if(! fieldDescribeTokens.get(field).getDescribe().isAccessible()){
                throw new System.NoAccessException();
                return null;
            }
        }
                
        //OK, they're cool, let 'em through
        return [SELECT Id, Name, Quantity__c, Price__c, Packed__c, CreatedDate FROM Camping_Item__c];
                
    }
    
    @AuraEnabled
    public static Camping_Item__c saveItem(Camping_Item__c item) {
        upsert item;
        return item;
    }

}

For the life of me I can't seem to figure out what is missing.  Any and all help will be deeply appreciated.  Thank you for your time!




 
_Zach Boyd_Zach Boyd
I was not able to put your code in my org to fully test, but I do see that you are calling helper​.validateCampingForm in the createCampingList function, but I do not see validateCampingForm defined in your helper. Could you try adding that into your helper or updating your code to not invoke it just so you can verify that is indeed the problem?
vleandrovleandro
My bad...I posted the wrong code all together in the campingListHelper.js.  Here is the right code.  I obviously had been starting at the screen way too long by that point.  <sigh>
 
({

     createItem : function(component, 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 items = component.get("v.items");
                items.push(response.getReturnValue());
                component.set("v.items", items);
            }
        });
        $A.enqueueAction(action);
		
	},
    
    validateCampingForm: function(componet) {
        //Simplistic error checking
        var isCampingValid = true;
        
        var nameField = component.find("campingName");
		var quantityField = component.find("quantity");
		var priceField = component.find("price");
		
        if(nameField.get("v.value") == '' ){
            nameField.set("v.errors",[{message:"name can't be blank"}]);
            isCampingValid = false;
        }
        else{
            nameField.set("v.errors",null);
        }
        
        
        if( quantityField.get("v.value") == '' ){
            quantityField.set("v.errors",[{message:"Quantity can't be blank"}]);
            isCampingValid = false;
        }
        else{
            quantityField.set("v.errors",null);
        }
        
        
        if(priceField.get("v.value") == ''){
            priceField.set("v.errors",[{message:"Price can't be blank"}]);
            isCampingValid = false;
        }
        else{
            priceField.set("v.errors",null); 
        }
        return(isCampingValid);
               
    }    
    
})

Thank you.
vleandrovleandro
Debugging is your friend.  I'm having to dust off my very old debugging tricks from back in the day but I got it sorted out.  It's very important that you spell component correctly.  Apparently the code doesn't like it when you spell it "compnent".  <heavy sigh>  But the good news is I pass the Challenge AND the code works!