[SalesForce] The campingList JavaScript controller isn’t setting the ‘item’ as a parameter or saving the record correctly

I have been through several posts on workarounds to this issue where the code works, but the challenge is not passed, none of the proposed solutions seem to work for me:

https://d2hwpln2xvs3s.cloudfront.net/forums/?id=906F0000000kDmtIAE
https://developer.salesforce.com/forums/?id=906F0000000kDPpIAM
https://developer.salesforce.com/forums/?id=906F0000000kDPpIAM

in the last one Jeff Douglas seems to mention some code change/enhancement to the trailhead module.

Here is my handleAddItem function from the CampingListController.js:

    handleAddItem: function(component, event, helper) {
    var addItm = event.getParam("item");
    helper.createItem(component, addItm);

},

and createItem from the helper:

createItem: function(component, newItem) {
    var action = component.get("c.saveItem");
    action.setParams({
        "item": newItem
    });
    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);
            component.set("v.newItem",{'sobjectType':'Camping_Item__c',
                               'Name': '',
                               'Quantity__c': 0,
                               'Price__c': 0,
                               'Packed__c': false});
        }
    });
    $A.enqueueAction(action);

enter image description here

enter image description here

I posted the images of the rendered component after submitting the form and the record that is created from the apex method to the DB. Any idea why the challenge is not passed?

Best Answer

I ended up moving the createItem function from my helper function to the CampingListController, since Trailhead does not "ask" us to split the createItem into a helper function.

handleAddItem: function(component, event, helper) {
    var addItm = event.getParam("item");
    helper.createItem(component, addItm);

},

    createItem: function(component, newItem) {
    var action = component.get("c.saveItem");
    action.setParams({
        "item": newItem
    });
    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);
},