[SalesForce] Simple Lightning Component Help with passing values to Custom Object

I am trying to build a lightning component that will be used on our Community site for article voting.

I have a custom object called Public_Article_Ratings__c with some simple fields I want to capture.

Here is the component markup:

   <aura:component controller="ArticleVotingController" implements="forceCommunity:availableForAllPageTypes" >
    <aura:attribute name="newVote" type="Public_Article_Rating__c"
                    default="{'sobjectType':'Public_Article_Rating__c',
                             'Article_ID__c':'',
                             'Article_URL__c':'',
                             'upVote__c':'',
                             'downVote__c':'',
                             'Comments__c':''
                             }"/>
<form>
   <div id="ratingArea"> 
       <span>Was this article helpful?</span>
       <ui:inputCheckbox aura:id="voteUp" label="Yes"
                      value="{!v.newVote.upVote__c}"/>

       <ui:inputCheckbox aura:id="voteDown" label="No"
                       value="{!v.newvote.downVote__c}" />

      <ui:inputTextArea aura:id="articleComments" label="Comments"
                     value="{!v.newVote.Comments__c"/>
        <br></br>
       <ui:button label="Submit" labelClass="label"
                press="{!c.createVote}"/>
    </div>
</form>

</aura:component>

The client side controller:

({
    createVote : function(component, event, helper) {
        var newVote = component.get("v.newVote");
        helper.createVote(component, newVote);
    }
})

The helper:

({
    createVote : function(component, vote) {
        this.upsertVote(component, vote, function(a){
            var votes = component.get("v.votes");
            votes.push(a.getReturnValue());
            component.set("v.votes", votes);
        });
},

    upsertVote : function(component, vote, callback){
        var action = component.get("c.saveVote");
        action.setParams({
            "vote": vote
        });
        if (callback) {
            action.setCallback(this, callback);
        }
        $A.enqueueAction(action);
    }
})

And the server-side controller:

public with sharing class ArticleVotingController {

    @AuraEnabled
    public static Public_Article_Rating__c saveVote(Public_Article_Rating__c vote){
        upsert vote;
        return vote;

    }

}

For now, I am just trying to capture the most simple fields to make sure it works (votes & comments). But I am getting an error in the console:

Looks like there's a problem: Unknown Error : Cannot read property
'push' of undefined

I was wondering if anyone with more Lightning Component building experience than me is able to pick out any flaws with the way I am trying to handle this. Thanks so much for reading.

Best Answer

1) You are pushing to newVote which is of type Public_Article_Rating__c it is not an array

2) Assuming you constructed your vote code based on this example

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/qs_aotp_app.htm

You are missing an array attribute of type Public_Article_Rating__c

Add this line to your component:

  <aura:attribute name="votes" type="Public_Article_Rating__c[]"/>

Update:

Component:

<aura:component controller="ArticleVotingcontroller" implements="forceCommunity:availableForAllPageTypes" >
    <aura:attribute name="votes" type="Account[]"/>
    <aura:attribute name="newVote" type="Account"
                    default="{'sobjectType':'Account',
                             'Name':'',
                             'prao6308__RSSFEEDUPDATER__c':'',
                             'AccountNumber':''
                             }"/>
<form>
   <div id="ratingArea"> 
       <span>Was this article helpful?</span>
       <ui:inputCheckbox aura:id="voteUp" label="Yes"
                      value="{!v.newVote.prao6308__RSSFEEDUPDATER__c}"/>
       <ui:inputText aura:id="accounname" label="name" value="{!v.newVote.Name}"/>
    <ui:inputNumber aura:id="articleComments" label="Comments"
                     value="{!v.newVote.AccountNumber}"/>
       <br/>
       <ui:button label="Submit" labelClass="label"
                press="{!c.createVote}"/>
    </div>
</form>

</aura:component>

Client side controller:

({
 createVote : function(component, event, helper) {
        var newVote = component.get("v.newVote");
     console.log(newVote);
        helper.createVote(component, newVote);
    }
})

helper:

({
createVote : function(component, vote) {
        this.upsertVote(component, vote, function(a){
            var accs = component.get("v.votes");
            accs.push(a.getReturnValue());
            console.log('here'+accs);
            component.set("v.votes", accs);
        });
},

    upsertVote : function(component, vote, callback){

        var action = component.get("c.saveVote");
        action.setParams({
            "vote": vote
        });
           //If upsert of the account is success I want to alert the account Id back to the user.
            action.setCallback(this, function(response) 
            {
                var state = response.getState();
                if (state === "SUCCESS") 
                {
                    alert("From server: " + response.getReturnValue());
                }
                else if (state === "ERROR") 
                {
                    var errors = response.getError();
                    if (errors) 
                    {
                        if (errors[0] && errors[0].message) 
                        {
                            console.log("Error message: " + 
                                     errors[0].message);
                        }
                    } 
                    else 
                    {
                        console.log("Unknown error");
                    }
                }
            });
        $A.enqueueAction(action);
    }
})

server side controller:

public class ArticleVotingcontroller {   @AuraEnabled
    public static String saveVote(Account vote){
        upsert vote;
        system.debug('#####'+ vote);
        return vote.Id;

    } }
Related Topic