2

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.

1 Answer 1

4

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;

    } }
8
  • Well, yesterday when I was testing this I was getting blank detail records. Now it seems like I cant submit anything to the object. Do you have any suggestions to get me moving forward?
    – Allen Mann
    Nov 4, 2015 at 17:12
  • @AllenMann I tested the code using account object, I will update the code in the answer take a look in a minute
    – Rao
    Nov 4, 2015 at 17:14
  • @AllenMann post your complete code if you still have issues I would love to see and debug whats going wrong
    – Rao
    Nov 4, 2015 at 17:51
  • Rao, thank you so much again for your help. It looks like this worked, and I learned some awesome new ways to debug my components. Is there any reason you took the second checkbox out of your sample? I only ask because now I am able to pass the UpVote value of "True" to my object but the second checkbox returns false when checked.
    – Allen Mann
    Nov 4, 2015 at 20:16
  • I was just testing checkbox component hence removed the second component no specific reason. I am learning lightning myself by looking at examples and answering questions here, so thanks to you too :).
    – Rao
    Nov 4, 2015 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .