0

like when i get return value from apex controller as a map(id,object) and i want to set it on an attribute. but its not happening. lengh is also not working.

lets say i have returned 5 object from apex controller, now

.cmp://only writing the attribute

<aura:attribute name="cartItemList" type="Object" />

JS controller:

var action = component.get('c.getCartItems');
            action.setParams({
              CartId : state.c__cartId
            });
            action.setCallback(this, function(response){
                var state = response.getState();
                alert(state);
                if(state =='SUCCESS' || state == 'DRAFT'){
                   alert('result data only ' + response.getReturnValue().length); //  **undefined**
                    var resultData=JSON.stringify(response.getReturnValue());
                    console.log('resultData from  apex check :' + JSON.stringify(response.getReturnValue()));
                    
                    var item=[];
                    for(var key in resultData){
                       item.push(resultData[key]); 
                    }
                    component.set('v.cartItemList',item);

             //**alert(item.length) is showing =1034**

kindly help.thanks in advance


thanks for answer quickly, but var itemList = Object.values(resultData); component.set('v.cartItemList',itemList); that also same thing happening alert(itemList.length); // returns 1019 but i want it to be 5

var action = component.get('c.getCartItems');
            action.setParams({
                CartId : state.c__cartId
            });
            action.setCallback(this, function(response){
                var state = response.getState();
                alert(state);
                if(state =='SUCCESS' || state == 'DRAFT'){
                    alert('result data only ' + response.getReturnValue().length); //undefined
                    var resultData=JSON.stringify(response.getReturnValue());
                    console.log('resultData from  apex check :' + JSON.stringify(response.getReturnValue()));
                    
                    /*var item=[];
                    for(var key in resultData){
                       item.push(resultData[key]); 
                    }*/
                    var itemList = Object.values(resultData);
                    component.set('v.cartItemList',itemList);
                    alert(itemList.length); ----1019
                    //component.set('v.cartItemList',item); 

I have also made <aura:attribute name="cartItemList" type="LIst" /> // stil not working showing 1019 items in UI.

I am pasting the result from apex after stringify.. enter image description here

now what i want is to show is 5 items in UI...with Name. like below.

CR-00006 CR-00007 CR-00008 CR-00009 CR-00010

5
  • A Map is returned from the server as a JavaScript object, with a property per key, not as an array.
    – Phil W
    Nov 11, 2020 at 20:40
  • What are you trying to do with the return values? There are a few choices, but what you're doing is not appropriate. The return value Map is an Object in JavaScript, so it won't have a length, nor does it need to be passed to JSON.stringify. What transformation are you trying to do?
    – sfdcfox
    Nov 11, 2020 at 20:48
  • I have added what i want to show in UI
    – kishore
    Nov 12, 2020 at 8:05
  • This lets say i have returned 5 object from apex controller assumption is creating the issue, can you share Apex code as well, what if you are returning 1019 records? Nov 12, 2020 at 15:00
  • no ,i have checked ,its returning 5 objects....and i have shared the resultData in console log ,please see below screenshot i have given.
    – kishore
    Nov 12, 2020 at 15:06

1 Answer 1

0

I believe you have the issue of iterating on the Stringification of your map.

Wouldn't this do the trick?

    var action = component.get('c.getCartItems');
    action.setParams({
        CartId : state.c__cartId
    });
    action.setCallback(this, function(response){
        var state = response.getState();
        alert(state);
        if(state =='SUCCESS' || state == 'DRAFT'){
            alert('result data only ' + response.getReturnValue().length); 
            //  **undefined** because it is an Object, and it does not have the "length" property

            var resultData = response.getReturnValue();
            console.log('resultData from  apex check :' + JSON.stringify(resultData));
                    
            var itemList = Object.values(resultData);
            component.set('v.cartItemList',itemList);

            alert(itemList.length); // should show the correct length, as itemList is now an array. 
        }
    });
    // enqueueAction, etc.

If you know that cartItemList is going to be a list... consider

    <aura:attribute name="cartItemList" type="List" />
1
  • please check i have have given the new code also. but not done anything
    – kishore
    Nov 12, 2020 at 10:24

You must log in to answer this question.

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