1

As part of these module, can you help me understanding the below logic

in order to display the expense from APEX, below is the logic used

({
    doinit : function(component, event, helper) {
        var action = component.get("c.getExpenses");
        action.setCallback(this, function (response) {
            var state = response.getState();
            if (component.isValid() && state == "SUCCESS") {
                // var expview = component.get("v.expenses");
                // var parseRes = JSON.parse(JSON.stringify(response.getReturnValue()));
                // expview.push(parseRes); // component.set("v.expenses",expview);
                component.set("v.expenses", response.getReturnValue());
            } else {
                console.log(state);
            }
        });
        $A.enqueueAction(action);
    }
})

Apex:code public class ExpensesController {

@AuraEnabled
public static List<Expense__c> getExpenses(){
    return [Select Id,Amount__c,Client__c,Date__c,Reimbursed__c,Name from Expense__c] ;       
} 

Aura Attribute expenses is getting set with the controller response (component.set("v.expenses",response.getReturnValue());). But If I want to use the commented lines, by doing a push and then set the component. It didn't work. WHy is that so ? I am doing a component get and then pushing the value which is the way even showed in the later section of this module while saving an expense record.

When to use the PUSH and when i can directly do a component.set?

can anyone help me in clarifying the doubts ??

  • Can you try using var parseRes = []; parseRes = JSON.parse(response.getReturnValue()); – Muzammil Bajaria Apr 15 '17 at 18:26
  • Why were you trying to parse the result? Isn't it already parsed when you use getReturnValue? – Renato Oliveira Apr 15 '17 at 18:27
  • Because I think you are returning list from apex controller which should be stored in array in javascript. And the response is already in stringified format – Muzammil Bajaria Apr 15 '17 at 18:28
  • @MuzammilBajaria: I tried by making parseRes as empty array and then put the result . but its the same... :(. I even tried by just using json.stringify ; still the same – Abhik Dey Apr 15 '17 at 18:33
  • @RenatoOliveira: i tried without parsing as well and wihtout stringify as well (all combinations) still it didn't the component attribuute. ..but while i use direct component.set instead of push ...it works...so not sure whats the difference in push – Abhik Dey Apr 15 '17 at 18:35
6

When you Push you are pushing a single value.

Your getExpenses returns a list so you would need to iterate over the results and push each iteration onto expenses.

In the trailhead module, the push is used as part of saveExpense which returns a single expense which is why pushing the returnValue works.

So when returning a list simply use component.set if you want to replace all values of expenses or if you want to add to iterate over the results pushing each one

I have not tried it but in principle when returning a list from the controller you should be able to add all values to the attribute using concat as such which would be additive to the existing values.

var expview = component.get("v.expenses");
var parseRes = response.getReturnValue(); //Assuming this is a list
expview.concat(parseRes);

  • nice explanation and i have verified it :)....i tried pushing one item to it and it worked...expview.push(expviewParse[0]); 'concat' i think nt available in lightnin... anwy thkns again. – Abhik Dey Apr 15 '17 at 19:05
  • @AbhikDey - did you try concat? Its a standard JS array method so it should be available – Eric Apr 15 '17 at 19:06
  • hi.. i tried, it didn work... – Abhik Dey Apr 15 '17 at 19:10

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.